大家下午好,
我对Postgresql很新,如果我的问题低于平均值,请提前道歉.
我正在以csv flay文件格式接收一些数据,很好地插入到临时表中以供我预览数据.我遇到问题的是数据字段存储为字符串值.例如,10Apr96 – 01Jan85
我写了下面的查询,看看是否可以打破字符串并以日期格式连接在一起.
有没有更简单的方法来实现我正在做的事情?
select concat(cast(substring(datetext,1,2) as varchar(2)),'/',case when cast(substring(datetext,3,3) as varchar(3)) = 'Jan' then '01' when cast(substring(datetext,3) as varchar(3)) = 'Feb' then '02' when cast(substring(datetext,3) as varchar(3)) = 'Mar' then '03' when cast(substring(datetext,3) as varchar(3)) = 'Apr' then '04' when cast(substring(datetext,3) as varchar(3)) = 'May' then '05' when cast(substring(datetext,3) as varchar(3)) = 'Jun' then '06' when cast(substring(datetext,3) as varchar(3)) = 'Jul' then '07' when cast(substring(datetext,3) as varchar(3)) = 'Aug' then '08' when cast(substring(datetext,3) as varchar(3)) = 'Sep' then '09' when cast(substring(datetext,3) as varchar(3)) = 'Oct' then '10' when cast(substring(datetext,3) as varchar(3)) = 'Nov' then '11' when cast(substring(datetext,3) as varchar(3)) = 'Dec' then '12' end,cast(substring(datetext,6,2) as varchar(2))) as dt from tbl_loading_horses_tmp
您可以使用
the
原文链接:https://www.f2er.com/postgresql/192171.htmlto_date()
function,其中包含如何解释输入的格式字符串,并返回一个日期值,该值应该存储在数据库中.
SELECT to_date('10Apr96','DDMonYY'); to_date ------------ 1996-04-10 (1 row)