Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert timestamp to date in Oracle SQL

How can we convert timestamp to date?

The table has a field, start_ts which is of the timestamp format:

'05/13/2016 4:58:11.123456 PM' 

I need to query the table and find the maximum and min timestamp in the table but I'm not able to.

Select max(start_ts)  from db  where cast(start_ts as date) = '13-may-2016' 

But the query is not returning any values.

Please help me in finding the max timestamp for a date.

like image 917
Dinu Avatar asked Jun 01 '16 04:06

Dinu


People also ask

Can we convert TIMESTAMP to date in SQL?

In SQL, timestamp is a function which is used to retrieve the current date and time of the SQL server without the database timezone offset. In SQL, CURRENT_TIMESTAMP is used to extract the current date and time. It takes no argument and returns the datetime value.

How do I convert a TIMESTAMP to a date?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

Can we convert date to TIMESTAMP in Oracle?

Oracle has expanded on the DATE datatype and has given us the TIMESTAMP datatype which stores all the information that the DATE datatype stores, but also includes fractional seconds. If you want to convert a DATE datatype to a TIMESTAMP datatype format, just use the CAST function.


2 Answers

CAST(timestamp_expression AS DATE) 

For example, The query is : SELECT CAST(SYSTIMESTAMP AS DATE) FROM dual;

like image 174
Peter Nosko Avatar answered Sep 28 '22 06:09

Peter Nosko


Try using TRUNC and TO_DATE instead

WHERE     TRUNC(start_ts) = TO_DATE('2016-05-13', 'YYYY-MM-DD') 

Alternatively, you can use >= and < instead to avoid use of function in the start_ts column:

WHERE    start_ts >= TO_DATE('2016-05-13', 'YYYY-MM-DD')    AND start_ts < TO_DATE('2016-05-14', 'YYYY-MM-DD') 
like image 26
Felix Pamittan Avatar answered Sep 28 '22 05:09

Felix Pamittan