Of the two queries below, which one is faster? The table contains more than 100M records. So I just want to know whether using ISNULL in the where clause is the same as first assigning a value to the variable and only using the variable in the where clause?
declare @dt datetime
select COUNT(*) from pandl
where PaymentDate >= ISNULL(@dt, convert(nvarchar(10),getdate(), 121))
select @dt = ISNULL(@dt, convert(nvarchar(10),getdate(), 121))
select COUNT(*) from pandl
where PaymentDate >= @dt
getdate()is evaluated multiple times over the course of the first query (I would have to run some tests to verify), since the convert rounds it to the day, it would only really matter if the query started before midnight and was still processing (and assigning new values togetdate()) after midnight. I totally agree with you that assigning once is better, just wanted to clarify when it can be really bad compared to just "not best."