2013-05-13 42 views
-2

我如何用null替换无效的日期时间?如何用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转换为Date这样:

enter image description here

运行上面我的SQL语句时,我得到:

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

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

+1

你为什么要把这些日子添加到1900而不是0001? 'SELECT DATEADD(DAY,734562,CAST('00010101'AS DATE))'工作正常并且返回'2012-03-01' – 2013-05-13 23:18:18

+0

@MartinSmith在sql server 0001中无效 – 2013-05-13 23:21:34

+0

是的(它有更新的数据类型)。请参阅编辑以前的评论。 – 2013-05-13 23:26:51

回答

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 

工程就像一个魅力!