This query is taking long time when endDate is null (i think that its about case statement, before case statement it was fast)
SELECT *
FROM HastaKurumlari
WHERE CONVERT(SMALLDATETIME,'21-05-2009',103)
BETWEEN startDate
AND (CASE WHEN endDate IS NULL THEN GETDATE() ELSE endDate END)
What should i use, when endDate is null to make it faster ?
Here’s the query without CONVERT or CASE:
To make sure Sql Server doens’t evaluate getdate() for every row, you could cache it, although I’m pretty sure Sql Server is smart enough by default:
Posting the query plan could help explain why the query is slow: