?
Solved

SQL Server Error 8115 - CONVERT varchar to datetime

Posted on 2002-05-23
4
Medium Priority
?
2,696 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 70

Accepted Solution

by:
Scott Pletcher earned 200 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 70

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 70

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

831 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