How can I find date difference between two dates in terms of seconds in PostgreSQL?
There is no function for giving date difference in terms of seconds like in SQL-Server:
DATE_DIFF(second, '2011-12-30 09:55:56', '2011-12-30 08:54:55')
Please help me to have this in PostgreSQL too
First, the dates need to be values of
timestamptype (so append::timestampif you’re just specifying them as string literals).If you subtract two timestamps, the result is of
intervaltype, which describes a duration of time (in hours, minutes, seconds etc.) You can useextract(epoch from interval_value)to convert the interval into an absolute number of seconds.So, putting that all together:
Remember that the
::timestampis only needed to convert the string literal to a timestamp: you don’t need it if you’re using the value of a timestamp column, for example.