A.A*_*idi 4 postgresql types timestamp
我的数据集包含以空格分隔的单独的年,月,日,小时,分钟和第二列:
+-------------------+
|2007|09|28|21|14|06|
|2007|09|28|21|14|06|
|2007|09|28|21|14|06|
|2007|09|28|21|14|06|
|2007|09|28|21|14|06|
+-------------------+
Run Code Online (Sandbox Code Playgroud)
我想在时间戳数据类型下将它们集成为单个列.我在timestamp data-type中创建了一个新列,并通过以下代码更新列:
Update s2
set dt = year || '-' || month || '-' || day
|| ' ' || hour || ':' || min || ':' || second
Run Code Online (Sandbox Code Playgroud)
但我面临以下错误:
ERROR: column "dt" is of type timestamp without time zone but expression is of type text
LINE 1: Update temp set dt= year || '-' || month || '-' || day ||...
^
HINT: You will need to rewrite or cast the expression.
********** Error **********
ERROR: column "dt" is of type timestamp without time zone but expression is of type text
SQL state: 42804
Hint: You will need to rewrite or cast the expression.
Character: 22
Run Code Online (Sandbox Code Playgroud)
而且,我可以通过varchar
数据类型预先进行集成.
你得到的答案解释错误:你需要转换text
到timestamp
明确.
但是,正确的解决方案是使用 to_timestamp()
UPDATE s2
SET dt = to_timestamp(year || '-' || month || '-' || day || ' '
|| hour || ':' || min || ':' || second
,'YYYY-MM-DD hh24:mi:ss');
Run Code Online (Sandbox Code Playgroud)
为什么?
普通模型'text'::timestamp
取决于日期/时间格式的本地设置,可能在一个安装中工作,但在另一个PostgreSQL安装中"突然"失败.保证给定的语句可以工作,与datestyle
设置和语言环境无关.
嗯,确切地说,example('YYYY-MM-DD hh24:mi:ss'
)中的模式符合ISO 8601(SQL标准),它对任何语言环境都有效.
归档时间: |
|
查看次数: |
1179 次 |
最近记录: |