Get a timestamp from concatenating day and time columns

一个人想着一个人 提交于 2019-11-29 03:38:21
SELECT EXTRACT(EPOCH FROM (Day || ' ' || Time)::timestamp);

date and time types

If your Day is of type date and your Time is of type time, there is a very simple solution:

SELECT EXTRACT(EPOCH FROM (day + time));

You can just add date and time to get a timestamp [without time zone] (which is interpreted according to the time zone setting of your session).

And, strictly speaking, extracting the epoch is unrelated to your question per se.
date + time result in a timestamp, that's it.

String types

If you are talking about string literals or text / varchar columns, use:

SELECT EXTRACT(EPOCH FROM ('2013-07-18' || ' ' || '21:52:12')::timestamp);

or

SELECT EXTRACT(EPOCH FROM cast('2013-07-18' ||' '|| '21:52:12' AS timestamp));

Your form does not work

SELECT EXTRACT(EPOCH FROM TIMESTAMP ('2013-07-18' || ' ' || '21:52:12'));

This would work:

SELECT EXTRACT(EPOCH FROM "timestamp" ('2013-07-18' || ' ' || '21:52:12'));

I quote the manual about type casts:

It is also possible to specify a type cast using a function-like syntax:

typename ( expression )

However, this only works for types whose names are also valid as function names. For example, double precision cannot be used this way, but the equivalent float8 can. Also, the names interval, time, and timestamp can only be used in this fashion if they are double-quoted, because of syntactic conflicts. Therefore, the use of the function-like cast syntax leads to inconsistencies and should probably be avoided.

Bold emphasis mine.
The gist of it: rather use one of the first two syntax variants.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!