views:

43

answers:

3

Hi,

How to query rows in a table where the created_date column value in the table falls on a weekend date ONLY, i.e. Saturday/Sunday using Oracle SQL..

Thanks

+1  A: 
WHERE to_char(created_date, 'D') >= 6

To improve the performance you could create function based index to_char(created_date, 'D')

zerkms
-1 not locale-safe; on my system, Saturday is 7 and Sunday is 1.
Jeffrey Kemp
depends on the setting of NLS_TERRITORY.
Jeffrey Kemp
Did not know that, thanks for correction ;-)
zerkms
+4  A: 
SELECT *
    FROM YourTable
    WHERE  TO_CHAR (created_date, 'DY') IN ('SAT', 'SUN')
Joe Stefanelli
+1  A: 

@Joe Stefanelli This is not absolutely correct, since the abbreviations returned are NLS-sensitive: the language in which month and day names and abbreviations are returned use the default date language for your session.

If you compare to-char result to literals in English (IN ('SAT', 'SUN')), it's better to precisely indicate it with nlsparams parameter, so the resulting query will look like this:

SELECT *
    FROM YourTable
    WHERE  TO_CHAR (created_date, 'DY', 'NLS_DATE_LANGUAGE=AMERICAN') IN ('SAT', 'SUN')
andr