How to trunc a date to seconds in Oracle

I am sorry, but all my predecessors seem to be wrong.

select cast(systimestamp as date) from dual 

..does not truncate, but rounds to the next second instead.

I use a function:

CREATE OR REPLACE FUNCTION TRUNC_TS(TS IN TIMESTAMP) RETURN DATE AS
BEGIN

    RETURN TS;

END;

For example:

SELECT systimestamp
    ,trunc_ts(systimestamp) date_trunc
    ,CAST(systimestamp AS DATE) date_cast 
FROM dual;

Returns:


    SYSTIMESTAMP                       DATE_TRUNC             DATE_CAST
    21.01.10 15:03:34,567350 +01:00    21.01.2010 15:03:34    21.01.2010 15:03:35

Since the precision of DATE is to the second (and no fractions of seconds), there is no need to TRUNC at all.

The data type TIMESTAMP allows for fractions of seconds. If you convert it to a DATE the fractional seconds will be removed - e.g.

select cast(systimestamp as date) 
  from dual;

Tags:

Sql

Oracle