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.
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.
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.
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.
CAST(timestamp_expression AS DATE)
For example, The query is : SELECT CAST(SYSTIMESTAMP AS DATE) FROM dual;
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With