Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL 2005 query error Expand / Collapse
Author
Message
Posted Friday, March 29, 2013 2:16 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 9:30 AM
Points: 403, Visits: 982
ColA -- nvarchar
SELECT * from TABLENAME where isdate(ColA)=1 and (ColA)<>'' and DATEDIFF(year,cast(ColA as datetime), GETDATE()) = 1

SELECT * from TABLENAME where isdate(ColA)=1 and (ColA)<>'' and CAST(ColA AS DATETIME) < DATEADD(yyyy, -1, GETDATE())

Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.

Any idea why i get this error?

If new datetime col is added and values are inserted from ColA then the above queries runs.

Post #1436839
Posted Friday, March 29, 2013 8:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:08 AM
Points: 13,228, Visits: 12,706
balasach82 (3/29/2013)
ColA -- nvarchar
SELECT * from TABLENAME where isdate(ColA)=1 and (ColA)<>'' and DATEDIFF(year,cast(ColA as datetime), GETDATE()) = 1

SELECT * from TABLENAME where isdate(ColA)=1 and (ColA)<>'' and CAST(ColA AS DATETIME) < DATEADD(yyyy, -1, GETDATE())

Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.

Any idea why i get this error?

If new datetime col is added and values are inserted from ColA then the above queries runs.



Because you have some values that can't be cast as datetime. You probably need to do this in two passes. Create a cte then filter it.

Something like this:


;with MyDates as
(
SELECT * from TABLENAME where isdate(ColA)=1
)

select * from MyDates
where DATEDIFF(year,cast(ColA as datetime), GETDATE()) = 1

Also there is no need for this: and (ColA) <> ''

The IsDate function already removes that.

select ISDATE('')



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436946
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse