【问题标题】:Regular Expression in redshift红移中的正则表达式
【发布时间】:2016-07-26 14:34:11
【问题描述】:

我有一个以以下格式输入的数据 -

2016-006-011 04:58:22.058

这是不正确的日期/时间戳格式,为了将其转换为正确的格式,如下所示 -

2016-06-11 04:58:22.058

我正在尝试在 redshift 中使用正则表达式来实现这一点。有没有办法使用正则表达式删除日期和月份部分中的额外零(0)。由于日期会有所不同,因此我需要更通用的东西,而不是仅针对此示例进行跟踪。

【问题讨论】:

  • Find (\d{4}-)\d?(\d{2}-)\d?(\d{2}) Replace $1$2$3 你必须定义通用日期格式。收集所有使用的日期格式,制作一个通用的正则表达式。

标签: regex amazon-web-services amazon-redshift


【解决方案1】:

函数regexp_replace()(参见documentation)应该可以解决问题:

select
    regexp_replace(
      '2016-006-011 04:58:22.058'  -- use your date column here instead
    , '\-0([0-9]{2}\-)0([0-9]{2})' -- matches "-006-011", captures "06-" in $1, "11" in $2
    , '-$1$2'                      -- inserts $1 and $2 to give "-06-11"
    )
;

所以结果是,根据需要:

     regexp_replace
-------------------------
 2016-06-11 04:58:22.058
(1 row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2022-06-23
    • 2016-12-14
    • 2019-04-22
    • 2011-10-06
    相关资源
    最近更新 更多