Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discard millisecond part from timestamp

How can I discard/round the millisecond part, better if the second part is also removed from a timestamp w/o timezone ?

like image 943
Dipro Sen Avatar asked Apr 18 '12 15:04

Dipro Sen


People also ask

How do I remove milliseconds from a timestamp?

Use the setSeconds() method to remove the seconds and milliseconds from a date, e.g. date. setSeconds(0, 0) . The setSeconds method takes the seconds and milliseconds as parameters and sets the provided values on the date.


1 Answers

A cast to timestamp(0) or timestamptz(0) rounds to full seconds:

SELECT now()::timestamp(0); 

Fractions are not stored in table columns of this type.

date_trunc() truncates (leaves seconds unchanged) - which is often what you really want:

SELECT date_trunc('second', now()::timestamp); 
like image 179
Erwin Brandstetter Avatar answered Oct 28 '22 17:10

Erwin Brandstetter