date
和time
类型
如果您Day
是date
类型和你Time
是time
类型,有一个非常简单的解决方案:
SELECT EXTRACT(EPOCH FROM (day + time));
您可以再补充date
和time
得到timestamp [without time zone]
(这是根据您的会话的时区设置的解释)。
严格来说,提取时代与您的问题本身无关。
date
+ time
导致timestamp
,就是这样。
字符串类型
如果你谈论的是字符串文字或text
/varchar
列,请使用:
SELECT EXTRACT(EPOCH FROM ('2013-07-18' || ' ' || '21:52:12')::timestamp);
或
SELECT EXTRACT(EPOCH FROM cast('2013-07-18' ||' '|| '21:52:12' AS timestamp));
你的形式不不工作
SELECT EXTRACT(EPOCH FROM TIMESTAMP ('2013-07-18' || ' ' || '21:52:12'));
这会工作:
SELECT EXTRACT(EPOCH FROM "timestamp" ('2013-07-18' || ' ' || '21:52:12'));
我引述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.
大胆重点煤矿。
它的要点:而是使用前两种语法变体之一。
你忘了提供确切的数据类型。 –