I am looking for what I think to be a very useful information regarding SQL Server Management Studio.
I have a table with a column of type varchar that stores dates, numbers, and strings.
Those dates are stored with the following format:
dd/mm/aaaa
I have a query that searchs form matching rows and one requirement is that the user must be able to seacrh between dates (period of time).
There is no mistery if I had just dates, I could use a query:
where convert(datetime,a.valor,103) between '01/01/2013' and '03/01/2013'
The problem is that this query fails when reach a row that the value is not a date.
What would be a efficient way to perform that query since there could be thousand of rows to search?
The typical answer is to add a WHERE clause:
However this is problematic in your situation for a couple of reasons:
ISDATE()won’t necessarily match the way you want depending on regional settings of the server, the user’s language or dateformat options, etc. For example:You can’t really control that SQL Server will try and perform the
CONVERTafter the filter.You can’t even use subqueries or CTEs to try and separate the filter from the CONVERT because SQL Server can optimize the operations in the query in whatever order it deems more efficient.
For example, with a limited sample, you will probably find that this works okay:
But I have seen cases with even this construct where SQL Server has tried to evaluate the filter first, leading to the same error you’re currently getting.
A couple of safer workarounds:
Add a computed column, e.g.
To protect yourself from possible misinterpretations at runtime, you should specify dateformat before issuing a query that references the computed column, e.g.
Create a view:
Again, you’ll want to issue a
SET DATEFORMATwhen querying the view.Use a temp table:
You may still want to use
DATEFORMATto protect yourself from conflicts betweenISDATEand user settings.And no, you should not try to validate your strings as dates using string pattern matching as was suggested in another (now deleted) answer:
You will have to have some pretty complex and heavy-handed validation there to handle all valid dates including leap years.