In SQL Server 2008 R2, I have to compare two dates of birth from different tables.
- column
C.date_of_birthis of typeDATETIME - column
R.dobis a date stored asVARCHAR, but with no leading zeroes on the month or day parts to make its length consistent (<–that’s the tough part).
I’ve gotten as close as this:
where R.dob <> convert(varchar, cast(C.date_of_birth as DATE), 101)
but that returns too many rows because 1/5/1923 in R.dob does not match 01/05/1923 in C.date_of_birth.
How can I format R.dob so it can correctly be compared to C.date_of_birth?
Thank you.
You can cast your varchar as a datetime. For instance, the result of this:
Would be
1923-01-05 00:00:00.000Then you can just use
DATEDIFF()to compare them to see which are equal to the day (or whatever interval you so desire).