troubleshooting Question

The Conversion of a varchar data type to datetime data type resulted in an out-of-range value

Avatar of mike1142
mike1142 asked on
Microsoft SQL ServerMicrosoft SQL Server 2008SQL
8 Comments1 Solution22109 ViewsLast Modified:
The data is stored in the database as col1 = 'yyyymmdd' and col2 = 'hh:mm' CAST(table.col1 + ' ' + table.col2 AS DATETIME) Produces the out of range error at some point. I know it is a date because CAST(col1 AS DATETIME) gives the error but CAST(col2 AS DATETIME) does not.

Tried CASE WHEN ISDATE(col1) = 1 THEN CAST(col1 AS DATETIME) ELSE col1 END but this results in the same error. Ideally I would like to see the problem value(s) if it cannot cast properly.

This is a simple select statement. I cannot do anything about the data except ignore it, convert it or show it as is.

Thanks
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros