If this is a sniffing parameter, try adding option(recompile) at the end of your request. I would recommend creating a stored procedure to encapsulate logic in a more manageable way. It is also agreed - why do you pass 5 parameters if you need only three, judging by the example? Can you use this query?
select TrustAccountValue from ( SELECT MAX (tal.trustaccountlogid), tal.TrustAccountValue FROM TrustAccountLog AS tal INNER JOIN TrustAccount ta ON ta.TrustAccountID = tal.TrustAccountID INNER JOIN Users usr ON usr.UserID = ta.UserID WHERE usr.UserID = 70402 AND ta.TrustAccountID = 117249 AND tal.TrustAccountLogDate < '3/1/2010 12:00:00 AM' group by tal.TrustAccountValue ) q
And for what you stand, you use an ambiguous date format, depending on the language settings of the user performing the request. For me, for example, this is January 3rd, not March 1st. Check this:
set language us_english go select @@language --us_english select convert(datetime, '3/1/2010 12:00:00 AM') go set language british go select @@language --british select convert(datetime, '3/1/2010 12:00:00 AM')
The recommended approach is to use the "ISO" format yyyymmdd hh: mm: ss
select convert(datetime, '20100301 00:00:00')
Piotr Rodak Apr 29 '10 at 21:07 2010-04-29 21:07
source share