Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

how to use date columns having time part in where clause

KalpataruFeb 25 2017 — edited Feb 27 2017

Hi ALL,

SQL> Select Banner from v$version;

BANNER

--------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

PL/SQL Release 11.2.0.4.0 - Production

CORE    11.2.0.4.0      Production

TNS for Linux: Version 11.2.0.4.0 - Production

NLSRTL Version 11.2.0.4.0 - Production

Create Table Statement as Follows

CREATE TABLE TEST_DATES

(

  CODE   NUMBER(4),

  DATES  DATE

);

Insert into TEST_DATES(CODE, DATES) Values(1, TO_DATE('02/25/2017 12:08:00', 'MM/DD/YYYY HH24:MI:SS'));

Insert into TEST_DATES(CODE, DATES) Values(2, TO_DATE('02/26/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));

COMMIT;

I have written two SELECT statements one is returning rows as per the requirement , but another is not working due to the time part present in the Date column in CODE 1.

1) 1st query.

Select * FROM Test_Dates

WHERE DATES >= TO_DATE('25/02/2017','dd/mm/rrrr')

      AND DATES <= TO_DATE('26/02/2017','dd/mm/rrrr');

OUTPUT

--------------

CODEDATES
    125/02/2017 12:08:00
    226/02/2017

2) 2nd Query.

Select * FROM Test_Dates

WHERE DATES >= TO_DATE('25/02/2017','dd/mm/rrrr')

      AND DATES <= TO_DATE('25/02/2017','dd/mm/rrrr');

OUTPUT

-------------

no rows selected.

SELECT * FROM NLS_DATABASE_PARAMETERS; nls parameters.

 

PARAMETERVALUE
NLS_LANGUAGEAMERICAN
NLS_TERRITORYAMERICA
NLS_CURRENCY$
NLS_ISO_CURRENCYAMERICA
NLS_NUMERIC_CHARACTERS.,
NLS_CHARACTERSETWE8ISO8859P15
NLS_CALENDARGREGORIAN
NLS_DATE_FORMATDD-MON-RR
NLS_DATE_LANGUAGEAMERICAN
NLS_SORTBINARY
NLS_TIME_FORMATHH.MI.SSXFF AM
NLS_TIMESTAMP_FORMATDD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMATHH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMATDD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY$
NLS_COMPBINARY
NLS_LENGTH_SEMANTICSBYTE
NLS_NCHAR_CONV_EXCPFALSE
NLS_NCHAR_CHARACTERSETAL16UTF16
NLS_RDBMS_VERSION11.2.0.4.0

How to use the date columns with time part in where condition ?

What is the correct condition to use in where clause BETWEEN AND OR >= <= OR = for better performance tuning of the query ?

Another question is that  -->   Is it correct to keep NLS_DATE_FORMAT as DD-MON-RR or DD-MON-RRRR is correct ?

Please describe the pros and cons of the NLS_DATE_FORMAT.

This post has been answered by Paulzip on Feb 25 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 27 2017
Added on Feb 25 2017
14 comments
11,562 views