Sql variable - datetime and string representation of datetime variable

I have a query that reacts too long when the search parameter is a date type varchar data type. However, if I convert varchar to a datetime variable, the query runs fine. For example:

It takes too much time.

select count(id) from names where updateddate > '1/5/2010' 

This is normal.

 declare @dateparam datetime set @dateparam = convert(datetime, '1/5/2010',102) select count(id) from names where updateddate > @dateparam 

Which reason is working fine and the other not?

+4
source share
1 answer

Because in the case of varchar it must convert to a date. This takes time and may interfere with the efficient use of indexes.

It is almost always better to use the correct type.

+5
source

Source: https://habr.com/ru/post/1308964/


All Articles