Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trunc a date to seconds in Oracle

Tags:

sql

oracle

This page mentions how to trunc a timestamp to minutes/hours/etc. in Oracle.

How would you trunc a timestamp to seconds in the same manner?

like image 887
Zizzencs Avatar asked Oct 01 '08 15:10

Zizzencs


People also ask

How do I truncate a timestamp?

If you want to TRUNC to the second, there are a few ways to do that. A couple of the simplest are using Date addition of 0 which will implicitly convert TIMESTAMP to a DATE, dropping the fractional seconds and then add 0 which, obviously, doesn't change the value. Then CAST that result back to a TIMESTAMP.

How do I truncate a date and time in SQL?

SQL Server SPID – What is it? » In Oracle there is a function (trunc) used to remove the time portion of a date. In order to do this with SQL Server, you need to use the convert function. Convert takes 3 parameters, the datatype to convert to, the value to convert, and an optional parameter for the formatting style.

How does TRUNC function work in Oracle?

The TRUNC (number) function returns n1 truncated to n2 decimal places. If n2 is omitted, then n1 is truncated to 0 places. n2 can be negative to truncate (make zero) n2 digits left of the decimal point.


1 Answers

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;
like image 74
Tony Andrews Avatar answered Oct 06 '22 22:10

Tony Andrews