Question

I am having query on the below sql. Please help me.

 select to_date(sysdate, 'DD MONTH YYYY') , to_date(sysdate, 'yyyy/mm/dd hh24:mi:ss')
 from dual where 
 to_date(sysdate, 'DD MONTH YYYY') < to_date(sysdate, 'yyyy/mm/dd');

1) to_date(sysdate, 'DD MONTH YYYY') this will give Date object without time (may be time is 00:00).
Am i correct ? If not how will i get only Date object without Time in it?
2) From the above query It seems to_date(sysdate, 'yyyy/mm/dd') is greater than to_date(sysdate, 'DD MONTH YYYY'). Why ??



Update


1) My aim in the above URL is to find out to_date function will only return date (or along with time) though format 'DD MONTH YYYY' is not mentioning the time chars(hh:mm..) in it.
2) From the response i got that to_date will return Date with time always though we didn't mention the time chars.
3) Finally to get only DATE we should use trunc() / to_date(to_char(sysdate,'mm-dd-yyyy'),'mm-dd-yyyy')

Check the below

select to_char(trunc(sysdate), 'yyyy/mm/dd hh24:mi:ss'), 
to_char(to_date(to_char(sysdate, 'yyyy-MM-dd'),'yyyy/mm/dd hh24:mi:ss'),'yyyy/mm/dd hh24:mi:ss') 
from dual;

-->hh24 :24 is important to get 00:00 format.

Was it helpful?

Solution

you've just shot yourself in the foot. its called implicit datatype conversion.

there is no TO_DATE(date) (which is what you're asking for) , so oracle has converted your query to TO_DATE(TO_CHAR(sysdate), 'DD MONTH YYYY') where TO_CHAR(sysdate) will pick up the default NLS format mask. depending on your default mask (see NLS_SESSION_PARAMETERS), your 1st may have resolved to the year 0012 and the 2nd to the year 0018. so 0012 is less than 0018.

if you want to truncate the sysdate, then just do trunc(sysdate)

OTHER TIPS

If you want to see the time part when selecting SYSDATE. You may set NLS_DATE_FORMAT before issuing a SELECT statement.

ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy-mm-dd hh24:mi:ss';

In IDEs (Today, PLSQL Developer, SQL Developer etc.) NLS_DATE_FORMAT can be set permanently to be used by every select statement you execute. In that case you dont need to run ALTER SESSION command prior to SELECT.

Ad. 1. In my test environment I got date with time:

select to_date('15 Maj 1998', 'DD MONTH YYYY') from dual;

Result (for Polish settings):

1998-05-15 00:00:00

Ad. 2. Why do you use sysdate when calling to_date? This function expects string describing datetime in given format. In my environment such call fails. You probably have other NLS settings so sysdate can be cast to string and such string gives you strange result. Use string, not sysdate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top