I have an app that uses a table that has a varchar column [BadColumn] which is populated by a date in the format MM/DD/YYYY or at least that's what most of the rows contain.
I have no control on modifying this table and changing the data type.
I need to report on this data to show rows that are within a week.
Here's my problem:
every time I use something to compare/filter the date I get an error
Conversion failed when converting date and/or time from character string
So here's what I have tried so far :
Tried to create a view with the
[BadColumn]being converted to aDatetype column[NewBadColumn]and convert the value from the original table to a date type usingConvert(date, BadColumn). The view gets populated, but now when I still try to query usingDateADDor do any date comparisons I get the same error.I have also tried to use
Convert(Date, BadColumn, 105), but same problem.I have tried other formats, but still the same issue cannot do a date comparison on that column.
I am not sure which row is throwing the error, how do I find it and fix this issue.
Thank you for the responses. I have tried using ISDate function to find the bad records. But I am still getting the same error "Conversion failed when converting date and/or time from character string." when I try to use DateAdd.
Here's the code I am using :
Select t.* from (Select * from dbo.BadTableName q with (nolock) where ISDate(BadColumn)=1 and ISDATE(BadColumn) Is Not Null ) t where t.BadColumn > DATEADD(dd,-2, GetDATE())
Any help is appreciated.