找出格式不同的不同日期之间的差异

我有两个表claims,其中有ptid,date_entered(date),以及customer_details,其中有ptid,first_transaction_date(varchar)。 claims表中输入的date_date的格式为YYYY-MM-DD,而customer_details表中输入的first_transaction_date的格式为YYYYMM。

目标:对于索赔表中所有唯一的ptid,我想查找first_transaction_date和date_entered天数之间的差异。我想看看在进入奖励计划两年之前,系统中已有什么客户。

我在以下方面遇到问题: 1.不同的数据类型 2.格式上的差异

这就是我所拥有的:

drop table if exists claims_1;

select a.*,b.diag_min,diag_min - first_month_active::date as start_dif
into claims_1
from claims as a
left join customer_details as b on a.ptid = b.ptid
where start_dif >= 0 and end_dif >= 0;

当我尝试使用str_to_date转换数据类型时,出现此错误:

(500310)无效的操作:函数str_to_date(字符变化,“未知”)不存在;

select date_format(str_to_date(first_month_active,'%Y%m'),'%Y-%m')from customer_details
miaobh 回答:找出格式不同的不同日期之间的差异

在postgresql中,要将字符串转换为日期,请使用to_date

 SELECT to_date('20190103','YYYYMMDD');

所以你需要

 SELECT to_date(first_month_active,'YYYYMM') from customer_details

而且,要将日期格式化为字符串,您需要to_char,这样最终查询将是

 SELECT to_char(to_date(first_month_active,'YYYYMM'),'YYYY-MM') from customer_details
本文链接:https://www.f2er.com/3159240.html

大家都在问