I’ve got a table with 9 million records. Each one has a “BirthDate” field that is stored as a varchar. I’m trying to select all where the birthdate is 25 years or less (all people 25 or under). It’s failing, because somewhere in this monstrosity of a table, there is an invalid value.
select COUNT(*) from LeadSplit where CAST(LeadSplit.Birthdate as datetime) > DATEADD(yy, -26, getdate()) and Birthdate is not null
The error is:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I’m at a loss as to how to find the row with the invalid value, and also, how to deal with it. I would like to just ignore it or fix it.
You could try to find the offending rows by doing something like:
This might not work for all cases – but it might give you a starting point.