I have an existing database that was setup (for whatever reason??) to store a date time as a string. I need to run a query for records that where entered between a specific date range but am not having any luck.
The string field is formatted like 1/25/2007 - 4:39 PM. I have tried the query below but of course it doesn’t work. Any help would be appreciated.
SELECT ID, CUSTOMER, ADD1, ADDZ, CITY, STATE, LASTD, ORDNUM
FROM dcar
WHERE (STATE = N'OR') AND (CAST(dcar.LASTD AS datetime) BETWEEN convert(smalldatetime, '10/01/2011', 101) AND convert(smalldatetime, '10/31/2011', 101))
PS – I have no idea why the developer decided to store dates this way!!!!
I think you should be able to cast the field as a datetime, if you remove the dash between the date and time with the
REPLACEfunction like so: