SQL 2005 query error

  • 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.

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply