Solved

SQL Server Error 8115 - CONVERT varchar to datetime

Posted on 2002-05-23
4
2,668 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:
ScottPletcher 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:ScottPletcher
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:ScottPletcher
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now