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!

TRUNC function for DATE

SM_308Dec 6 2007 — edited Dec 6 2007

Another thread from today made me think of the basics of TRUNC function used for DATEs. I am confused about what it acutally does.

This is what Oracle Documenations says about TRUNC function without format (like 'DD-MON-YY')
"If you omit fmt, then date is truncated to the nearest day" (Oracle 10Gr2 SQL reference, B14200-02)

SQL> alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS';

Session altered.

Query 1.

SQL> SELECT SYSDATE FROM DUAL;

SYSDATE
--------------------
06-DEC-2007 14:22:01



Query 2.

SQL> SELECT TRUNC(SYSDATE) FROM DUAL;

TRUNC(SYSDATE)
--------------------
06-DEC-2007 00:00:00

I have three questions

1)Since it 2:22 pm , shouldn't the result of TRUNC(SYSDATE) ie Query 2. be 7-Dec-2007 ?

  1. What difference does TRUNC function make in the code apart from setting the time to 00:00:00?

  2. Is it safer to add TRUNC for DATEs in the code?

SQL> select sysdate-1 from dual;

SYSDATE-1
--------------------
05-DEC-2007 14:27:51

SQL> select trunc(sysdate) - 1 from dual;

TRUNC(SYSDATE)-1
--------------------
05-DEC-2007 00:00:00

Added third question
Message was edited by:
user609308

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2008
Added on Dec 6 2007
6 comments
1,072 views