Need to convert the following oracle queries to MySQL queries.
1)
select to_char(to_date(?,'DD-MM-YY'))
from dual;
2)
select to_char(next_day(to_date(?,'DD-MM-YY'),'FRIDAY'))
from dual;
3)
select to_char(to_date(?,'DD-MM-YY'),'DAY')
from dual;
Best Answer
Your second query can be converted to MySQL in two ways.
First one is simple query, that adds
interval
of days to compared date, depending on whichdayofweek
it is. It's not the most elegant solution, and different days of week require different values (or selects). Code below usesCURDATE
as a starting value, query for each column can be used separately:Code found and copied from old GizmoLA post's comment. Similar one, just for Sunday, was explained on StackOverflow.
Second one is more universal, but requires creating function. In this case it's code could look like this one, found on SQLines post:
Usage (show next sunday):
You can also use NEXT_DAY and SUBDATE functions to get the previous date: