将具有日期和纪元格式值的字符串列转换为Postgresql / Tableau准备中的日期列

问题描述

如何将具有日期和纪元格式值的字符串列转换为日期列?我正在使用Postgresql / Tableau准备环境。

Please find attached the example here

确定此查询有效:

SELECT case 
WHEN len(d.properties__renewal_date__value) > 10 
THEN (timestamp 'epoch' + CAST(NULLIF(d.properties__renewal_date__value,'') AS BIGINT)/1000 * interval '1 second')
ELSE to_date(d.properties__renewal_date__value,'DD/MM/YYYY') end as renewal_date

解决方法

这里是@AdrianKlaver的评论的编码。首先确定具有正则表达式匹配项的当前值的类型,然后进行相应的转换。

select case 
   when "renewal date" ~ '^\d+$' then to_timestamp("renewal date"::double precision)::date
   else to_date("renewal date",'dd/mm/yyyy')
 end as renewal_date
 from _table;

红移风味,历元值(以毫秒为单位):

select case 
  when "renewal date" ~ '^\d+$' then 
    ('1970-01-01'::date + ("renewal date"::double precision/1000) * '1 second'::interval)::date
  else 
    to_date("renewal date",'dd/mm/yyyy')
  end as renewal_date
  from _table;