如何用NULL替换溢出?

时间:2013-05-13 23:14:58

标签: sql sql-server tsql sql-server-2008-r2

如何用null替换无效的日期时间?

我正在对INT列进行一些实际上是日期表示的操作:

select 


DATEADD(DD, rbaging.billing_period_end - 693594, '1900-01-01'),
DATEADD(DD, rbaging.billing_period_apply - 693594, '1900-01-01'),

*


 from 

[kslap208].[c021]..RB_Resident_Aging  rbaging

join
[kslap208].[c021]..PA_Profile_BASE_1119 pro
on rbaging.bill_to_profile_id=pro.profile_id   --4242

join 
[kslap208].[c021]..PA_Admit_Det_BASE_10 unit
on unit.profile_id=rbaging.bill_to_profile_id

rbaging表作为应该是日期的整数字段。这是一个样本:

734562
734776
734837

文档规定以这种方式将INT转换为日期:

enter image description here

当我在上面运行我的sql语句时,我得到了:

Msg 517, Level 16, State 1, Line 1
Adding a value to a 'datetime' column caused overflow.

如何用null替换无效的日期时间?

1 个答案:

答案 0 :(得分:0)

case when (rbaging.billing_period_end - 693594)>1
then DATEADD(DD, rbaging.billing_period_end - 693594, '1900-01-01')
else '' end,

case when (rbaging.billing_period_apply - 693594)>1
then DATEADD(DD, rbaging.billing_period_apply - 693594, '1900-01-01')
else ''
end

就像一个魅力!