【问题标题】:Convert dates in String data type with different formats column to Date data type in PostgreSQL将不同格式列的String数据类型的日期转换为PostgreSQL中的日期数据类型
【发布时间】:2023-04-10 02:53:01
【问题描述】:

我有一个包含日期的 String ( varchar ) 类型的数据库表。这些日期采用以下格式(示例):

15-06-2018
15/06/2018
15-Jun-2018
15 June 2018
June 15, 2018
June, 15, 2018

是否有任何查询可用于使用 PostgreSQL 查询从中获取 Date 数据类型值?

感谢您的帮助。

【问题讨论】:

  • 你有机会修复这个损坏的数据模型吗?

标签: sql postgresql date


【解决方案1】:

您可以使用大写表达式:

(case when col ~ '^[0-9]{2}-[0-9]{2}-[0-9]{4}$' then to_date(col, 'DD-MM-YYYY')
      when col ~ '^[0-9]{2}/[0-9]{2}/[0-9]{4}$' then to_date(col, 'DD/MM/YYYY')
      when col ~ '^[0-9]{2}-[A-Za-z]{3}-[0-9]{4}$' then to_date(col, 'DD Mon YYYY')
      . . .
 end) as the_date
  
  

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 2021-04-10
    • 2017-07-15
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多