将表示时间戳的字符串转换为PostgreSQL中的实际时间戳?

mum*_*mum 6 postgresql datetime database-design timestamp

在PostgreSQL:我转换stringtimestampto_timestamp():

select * from ms_secondaryhealthcarearea
where to_timestamp((COALESCE(update_datetime, '19900101010101'),'YYYYMMDDHH24MISS') 
    > to_timestamp('20121128191843','YYYYMMDDHH24MISS')
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

ERROR:  syntax error at end of input
LINE 1: ...H24MISS') >to_timestamp('20121128191843','YYYYMMDDHH24MISS')
                                                                       ^
********** Error **********

ERROR: syntax error at end of input
SQL state: 42601
Character: 176
Run Code Online (Sandbox Code Playgroud)

为什么?如何转换stringtimestamp

ppe*_*rka 7

一个太多的开口括号.试试这个:

select * 
from ms_secondaryhealthcarearea 
where to_timestamp(COALESCE(update_datetime, '19900101010101'),'YYYYMMDDHH24MISS') >to_timestamp('20121128191843','YYYYMMDDHH24MISS')
Run Code Online (Sandbox Code Playgroud)

你在to_timestamp有两个左括号:

where to_timestamp((COA.. -- <-- the second one  is not needed!
Run Code Online (Sandbox Code Playgroud)


Erw*_*ter 6

@ppeterka指出了语法错误.

更迫切的问题是:为什么要将timestamp数据存储为字符串?如果您的情况允许,请考虑将列转换为正确的类型:

ALTER TABLE ms_secondaryhealthcarearea
ALTER COLUMN update_datetime TYPE timestamp
USING to_timestamp(update_datetime,'YYYYMMDDHH24MISS');
Run Code Online (Sandbox Code Playgroud)

或使用timestamptz- 取决于您的要求.