Solved

SQL Server Error 8115 - CONVERT varchar to datetime

Posted on 2002-05-23
4
2,677 Views
Last Modified: 2007-12-19
The following SELECT statement works fine:
SELECT * FROM MyTable WHERE ISDATE(MyStringField) = 1 AND CONVERT(datetime, MyStringField) < GETDATE()

However, the following UPDATE fails:
UPDATE MyTable SET StatusField = 'Overdue' WHERE ISDATE(MyStringField) = 1 AND CONVERT(datetime, MyStringField) < GETDATE()

The error message is:
Server: Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
The statement has been terminated.


Is there a way to figure out which row is causing this problem and can anyone suggest how I can modify my statement to fix this error?
0
Comment
Question by:FrenchJericho
  • 3
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 50 total points
ID: 7029739
As far as changing the statement to make it work, I think this will do it:

UPDATE MyTable
SET StatusField = 'Overdue'
WHERE CASE WHEN ISDATE(MyStringField) = 1
           THEN CONVERT(datetime, MyStringField)
           ELSE GETDATE() END  < GETDATE()

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 7029747
As far as determining which row is causing the problem, the following query should give you all rows that could potentially cause the problem.  I'm not sure how to nail it down to a specific one:

SELECT *
FROM MyTable
WHERE ISDATE(MyStringField) = 0
--AND MyStringField > ' '  --include this if desired to eliminate NULL and empty strings
0
 

Author Comment

by:FrenchJericho
ID: 7029803
You suggestion works like a charm! Thanks.

However, I was checking for ISDATE(MyStringField) = 1 in my original query so I don't understand why it didn't work. Anybody can help me understand?
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 7030176
I don't understand why it didn't work since it's connected by an AND condition:

"WHERE ISDATE(MyStringField) = 1 AND CONVERT(datetime, MyStringField) < GETDATE()"

Once SQL determines that the ISDATE is false, it should not even evaluate the CONVERT -- they both can't be true ("... AND ...") if the first one is not true.  Usually SQL optimizers are very good at recognizing these situations and exploiting them to do as little calcuation/retrieval as is absolutely necessary.

If the connector were "OR", it would have to evaluate both, and I could see the error occuring.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Quer 4 23
Error when creating a table from a function 6 20
xml files 7 29
interpreting data from function COLUMNS_UPDATED 2 18
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question