Solved

Convert varchar to TimeDate SQL SERVER out-of-range

Posted on 2014-03-26
4
1,732 Views
Last Modified: 2014-03-26
Hi I previously posted a question about this issue before, the answers I got did fix the problem but its reared its ugly head again.  Have a to and from date which gets passed into the stored procedure and then I use them to count the records with an incident date between the to and from dates passed in, if they meet other conditions with in the statement.

The incident date in the database table is a varchar( I cant change this)

The code is below :-

select COUNT(pkCATSafetyAreaID) AS 'Count', T1.Category, T1.CategoryID from tblIncidentCATSSafetyAreas T1
INNER JOIN tblIncidentCATSUnsafeDetails T2 ON PATINDEX('%,'+CAST(T1.pkCATSafetyAreaID AS varchar(200))+',%',','+T2.SafetyAreas+',') > 0
inner join tblIncident T3 on T2.fkIncidentID = T3.pkIncidentID
inner join Admin_System.dbo.tblLocation T4 on T3.fkLocationID = T4.pkLocationID
WHERE Category = 'Personal Protective Equipment (PPE)'
AND CONVERT(datetime, t3.IncidentDate,105) BETWEEN CONVERT(datetime, @FromDate,105) AND CONVERT(datetime, @ToDate,105)
AND PATINDEX('%,'+cast(@pkLocationID as varchar(100))+',%',Sortkey) > 0
AND T4.Type LIKE '%'+@locationType+'%'
AND t3.ArchivedOn IS NULL
GROUP BY pkCATSafetyAreaID, T1.Category, T1.CategoryID, SafetyArea

The error I get when this is run is :- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

Any help with this matter would be much appreciated.
0
Comment
Question by:Sevron
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
>The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Just for kicks and giggles, execute the below (change to suit your needs) and tell us if you see any values that can't be converted to a date.   There are likely some non-date values that you have to deal with.

SELECT IncidentDate
FROM tblIncident
WHERE ISDATE(IncidentDate) = 0

-- repeat everywhere else you're trying to convert a date
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
The incident date in the database table is a varchar
Your error message clearly shows that this is not true.

You can use the ISDATE() function to filter the valid datetime values. Caveat: Due to optimization this may involve a helper table to filter the valid values first. E.g.

 
SELECT  * ,
        ISDATE(IncidentDate) AS IsValidDateTime
FROM    tblIncident;

Open in new window

0
 

Author Closing Comment

by:Sevron
Comment Utility
You sir are a legend!!! bad data in the column due to another development adding data into the table.

Cheers
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Once you have experience as an ETL developer, you learn to never trust source data, and to validate the hell out of it before inserting it into your databases.

Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Stored procedure with a parameter 6 20
how to remove non-duplicated row 5 25
SQl query 19 12
SQL Maintenance Plan 3 13
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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