May 30, 2013 at 8:20 am
Hi,
Think I'm overlooking the obvious I have this statement:
select *, '20' as [Yrs of Serv]
from cte where [Due Date for 20 years of Service Award] >= dateadd(month, datediff(month,'19000101',CURRENT_TIMESTAMP + 90),'19000101')
I need to run it for April like this:
select *, '20' as [Yrs of Serv]
from cte where [Due Date for 20 years of Service Award] >= dateadd(month, datediff(month,'19000101','20130401' + 90),'19000101')
But when I change out the 'CURRENT_TIMESTAMP' I get "Arithmetic overflow error converting expression to data type datetime"
What am I doing wrong?
May 30, 2013 at 8:26 am
i think it's the +90 INSIDE the datediff: '20130401' + 90 probably turns into either 20130491 or '2013040190', either of which are not valid datetimes.
maybe like this?
select *, '20' as [Yrs of Serv]
from cte
where [Due Date for 20 years of Service Award]
>= dateadd(
month,
datediff(month,'19000101','20130401') + 90 ,
'19000101')
Lowell
May 30, 2013 at 8:29 am
Try changing
'20130401' + 90
to
CAST('20130401' AS DATETIME) + 90
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537May 30, 2013 at 8:51 am
Using the "+X" shorthand way of adding days to a date is not the best practice, and in this case it is the cause of your error. It is always preferable to use DATEADD().
dateadd(month, datediff(month,'19000101', dateadd(day, 90, '20130401')),'19000101')
May 30, 2013 at 9:05 am
Awesome thanks guys! Much appreciated.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply