SQL Server – Searching Between Dates on a CHAR Type Field

sql server

I have a table that has a field in which dates are stored, the problem is that the field type is CHAR(10) and I'm being asked to query for data between certain dates.

I don't want to change the field type because I'm not sure how's that gonna affect the frontend application.

Is there a way of querying for results between dates without making changes to the data type?

Regards.

Best Answer

You can cast your date to perform where clause on it just like this.

WHERE CONVERT(DATETIME, MyStringDateField, 120) BETWEEN @DateParam1 AND @DateParam2

The last parameter in CONVERT is the date format. You must choose the one that match your text data of your date. You can find the list here

You must be aware that if a string does not match a date for a particular record, your query will fail with a message saying that the data XYZ cannot be converted to DATETIME type.