Determine if Oracle date is on a weekend?

前端 未结 5 919
南笙
南笙 2020-12-03 01:50

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         


        
相关标签:
5条回答
  • 2020-12-03 02:01
    MOD(TO_CHAR(my_date, 'J'), 7) + 1 IN (6, 7)
    

    is NOT correct! The day number of weekend days may be 6 & 7, or 7 and 1, depending on the NLS-settings.

    So the PROPER test (irrespective of NLS-settings) is this one mentioned before:

    TO_CHAR (my_date, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN')
    
    0 讨论(0)
  • 2020-12-03 02:10

    set NLS_TERRITORY before

    alter session set NLS_TERRITORY=SWEDEN;
    

    So you are sure which numbers are weekends

    0 讨论(0)
  • 2020-12-03 02:13

    In my opinion the best option is

    TO_CHAR (my_date, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN')

    0 讨论(0)
  • 2020-12-03 02:16

    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);
    
    0 讨论(0)
  • 2020-12-03 02:16

    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, 'ffffd') --- 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.

    0 讨论(0)
提交回复
热议问题