Redshift 中的有限选项
regexp_replace(hour, '(^\\d{4}-\\d{2}-\\d{2})-(\\d{2}:\\d{2}:\\d{2}$)', '\\1 \\2') AS a
regexp_replace(hour, '(^\\d{4}-\\d\\d-\\d\\d)-(\\d\\d:\\d\\d:\\d\\d)$', '\\1 \\2') AS b
regexp_replace(hour, '(^[\\d-]{10})-([\\d:]+)$', '\\1 \\2') AS c
left(hour,10) || ' ' || substring(hour FROM 12) AS e
现代 Postgres (9.1+) 中的更多选项
regexp_replace(hour, '(^\d{4}-\d{2}-\d{2})-(\d{2}:\d{2}:\d{2}$)', '\1 \2') AS a
regexp_replace(hour, '(^\d{4}-\d\d-\d\d)-(\d\d:\d\d:\d\d)$', '\1 \2') AS b
regexp_replace(hour, '(^[\d-]{10})-([\d:]+)$', '\1 \2') AS c
reverse(regexp_replace(reverse(hour), '-', ' ')) AS d
left(hour,10) || ' ' || right(hour, -11) AS e
overlay(hour placing ' ' from 11) AS f
to_timestamp(hour, 'YYYY-MM-DD-HH24:MI:SS') AS ts
SQL Fiddle.
按出现顺序从“限制性”到“廉价”。 ts 很特别。
一个
这就像currently accepted answer by @Zeki,在开头和结尾都加上了锚点^ 和$,以使其更加不模糊并且可能更快。
您希望 \d 的特殊含义为 class shorthand 用于数字。
在 Postgres 中,不要使用 \\ 转义反斜杠 \。除非您使用早已过时的非默认设置 standard_conforming_strings = off 运行,否则这是不正确的。
Redshift 卡在了一个旧的开发阶段,就是这样做的。除非用另一个反斜杠转义,否则反斜杠会被解释。
b
\d\d 比\d{2} 更短更便宜。
c
使用字符类进行简化:数字 + 连字符:[\d-] 和数字 + 冒号:[\d:]。
d
由于regexp_replace()没有第4个参数'g'只替换第一个匹配,你可以reverse()字符串,替换第一个连字符和reverse()后面。
在 Redshift 中不起作用,因为它使用 总是 替换所有出现的 simpler version of regexp_replace()。
e
如果格式固定如图所示,只需取前 10 个字符、一个空格和字符串的其余部分。
Redshift 使用不接受负参数的simpler versions of left() and right(),所以我用substring() 代替。
f
或者,更简单一点,只是 overlay() 第 11 个字符加一个空格。
Not implemented in Redshift.
ts
与其他类型不同,to_timestamp() 返回正确的 timestamp with time zone 类型,而不是 text。您也可以将结果分配给timestamp without time zone。 Details.。如果你想转换你的字符串,到目前为止最好的选择。
Not implemented in Redshift.