Solved

SQL Server Error 8115 - CONVERT varchar to datetime

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 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

10 Experts available now in Live!

Get 1:1 Help Now