问题描述
- SQL数据抽取,出现数据类型转换错误,求大神帮忙看一下。
-
select a.patient_id+'_'+cast(a.item_no as varchar(20))+'_'+cast(a.ledger_sn as varchar(20)) pk_pay,null pk_dcpv,null pvcode,'SDDYRMYY_G' code_group,'SDDYRMYY' code_org ,
cheque_type code_pay_mode,b.name name_pay_mode,a.charge amount,convert(varchar,a.dcount_date,120) date_pay, 'O' code_pvtype,
case when a.charge>=0 then 5 when a.charge<0 then 2 end dt_paytype,a.dcount_id code_psn_pay,psn.name name_psn_pay,mz_dept_no code_dept_pay
from mz_deposit_b a
left join zd_cheque_type b on a.cheque_type = b.code
left join a_employee_mi psn on psn.code = a.dcount_id
--WHERE a.dcount_date between ${BEGIN_DATE} and ${END_DATE}where EXISTS
(
select patient_id,times
from uh_dc_pi_temp t
where t.patient_id=a.patient_id
)
union all
select a.patient_id+'_'+cast(a.item_no as varchar(20))+'_'+cast(a.ledger_sn as varchar(20)) pk_pay,'O' code_pvtype,
a.patient_id+'_'+convert(varchar,times) pk_dcpv,a.patient_id+convert(varchar,times) pvcode,'SDDYRMYY_G' code_group,'SDDYRMYY' code_org ,
cheque_type code_pay_mode,b.name name_pay_mode,a.charge amount,convert(varchar,a.price_date,120) date_pay,
1 dt_paytype,a.price_opera code_psn_pay,psn.name name_psn_pay,mz_dept_no code_dept_pay
from gh_deposit_b a
left join zd_cheque_type b on a.cheque_type = b.code
left join a_employee_mi psn on psn.code = a.price_opera
--WHERE price_date between ${BEGIN_DATE} and ${END_DATE}
where EXISTS
(
select patient_id,times
from uh_dc_pi_temp t
where t.patient_id=a.patient_id
)union all
select a.patient_id+'_'+cast(a.item_no as varchar(20))+'_'+cast(a.ledger_sn as varchar(20)) pk_pay,null pk_dcpv,null pvcode,'SDDYRMYY_G' code_group,'SDDYRMYY' code_org ,
cheque_type code_pay_mode,b.name name_pay_mode,a.charge amount,convert(varchar,a.dcount_date,120) date_pay, 'O' code_pvtype,
case when a.charge>=0 then 5 when a.charge<0 then 2 end dt_paytype,a.dcount_id code_psn_pay,psn.name name_psn_pay,mz_dept_no code_dept_pay
from mz_deposit_c a
left join zd_cheque_type b on a.cheque_type = b.code
left join a_employee_mi psn on psn.code = a.dcount_id
--WHERE a.dcount_date between ${BEGIN_DATE} and ${END_DATE}where EXISTS
(
select patient_id,times
from uh_dc_pi_temp t
where t.patient_id=a.patient_id
)
union all
select a.patient_id+'_'+cast(a.item_no as varchar(20))+'_'+cast(a.ledger_sn as varchar(20)) pk_pay,'O' code_pvtype,
a.patient_id+'_'+convert(varchar,times) pk_dcpv,a.patient_id+convert(varchar,times) pvcode,'SDDYRMYY_G' code_group,'SDDYRMYY' code_org ,
cheque_type code_pay_mode,b.name name_pay_mode,a.charge amount,convert(varchar,a.price_date,120) date_pay,
1 dt_paytype,a.price_opera code_psn_pay,psn.name name_psn_pay,mz_dept_no code_dept_pay
from gh_deposit_c a
left join zd_cheque_type b on a.cheque_type = b.code
left join a_employee_mi psn on psn.code = a.price_opera
--WHERE price_date between ${BEGIN_DATE} and ${END_DATE}
where EXISTS
(
select patient_id,times
from uh_dc_pi_temp t
where t.patient_id=a.patient_id
)
之前我使用的是convert,会出现‘从数据类型 varchar 转换为 numeric 时出错。错误。改成cast后也是这样。
附:patient_id---char(12),.item_no--smallint,ledger_sn--smallint.
解决方案
这个就是因为字符串字段中的值有不是数字的记录
要么将存在的记录修改好,要么自己写一个转换数字的函数,把非数字的字符串转成null或者0这样