Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if Oracle date is on a weekend?

Is this the best way to determine if an Oracle date is on a weekend?

select * from mytable
where 
TO_CHAR (my_date, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN');
like image 784
Marcus Leon Avatar asked Aug 10 '10 16:08

Marcus Leon


People also ask

Where is Saturday and Sunday in Oracle SQL?

Absent a calendar table, you can use "CONNECT BY LEVEL <= n" (or similar techniques) to generate a result set containing all the dates between 2 given dates, use TO_CHAR to identify the Saturdays and Sundays, remove the others, and count how many rows are left.

How do you identify weekends in SQL?

Use the DATENAME() function and specify the datepart as weekday . select ID, Name, Salary, Date from dbo. yourTable where datename(weekday, Date) in ('Saturday', 'Sunday');

How do you find the day of the week for any date in Oracle?

You can use TO_CHAR( date_value, 'D' ) to get the day-of-week.

Does Oracle have a datediff function?

Use the @DATEDIFF function to calculate the difference between two dates or datetimes, in days or seconds. The difference between the specified dates. Valid values can be: DD , which computes the difference in days.


2 Answers

As of Oracle 11g, yes. The only viable region agnostic alternative that I've seen is as follows:

SELECT *
FROM mytable
WHERE MOD(TO_CHAR(my_date, 'J'), 7) + 1 IN (6, 7);
like image 191
ninesided Avatar answered Sep 24 '22 14:09

ninesided


Not an answer to the question. But some more information. There are many more SQL tricks with date.

to_char(sysdate, 'd') --- day of a week, 1,2,3 .. to 7
to_char(sysdate, 'dd') --- day of a month, 1,2,3 .. to 30 or 31
to_char(sysdate, 'ddd') --- day of a year, 1,2,3 .. to 365 or 366
to_char(sysdate, 'w') --- week of a month, 1,2,3,4 or 5
to_char(sysdate, 'ww') --- week of a year, 1,2,3 .. to 52

More here: to_char function.

like image 23
Guru Avatar answered Sep 22 '22 14:09

Guru