Search code examples
sqlpostgresqlsql-updatetruncatetimestamp-with-timezone

How to truncate the seconds of a timestamp column?


i have a table with a date column in timestampz data type, example:

Table "power"

date
2021-04-07 18:00:00
2021-04-07 18:01:01
2021-04-07 18:02:00
2021-04-07 18:03:00
2021-04-07 18:04:01

Some of the dates have an extra second at the end and i want to truncate it, so it looks like this

date
2021-04-07 18:00:00
2021-04-07 18:01:00
2021-04-07 18:02:00
2021-04-07 18:03:00
2021-04-07 18:04:00

I tried UPDATE power SET date = DATE_TRUNC('second', date) FROM power; but it isn't working, hope someone can help me with this, thanks in advance.


Solution

  • Your code should work if you use 'minute', but you really don't want the FROM clause:

    UPDATE power
        SET date = DATE_TRUNC('minute', date);
    

    Here is a db<>fiddle.