Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Slow SP with date parameters solved.

By Virgilio Licovali Bustos Ramirez,

When i use datetime fields in the where clause like timestampfield > {ts '2010-01-01 00:00;00'} and timestampfield < {ts '2010-01-31 23:59:59.998'} or timestampfield between {ts '2010-01-01 00:00:00'} and {ts '2010-01-31 23:59:59'} the query runs fine, but if i need to use it on stored procedeures like parameters like this:


@d1 as datetime,

@d2 as datetime



select * from tablewithdates where datetimefield between @d1 and @d2



The Procedure becomes slow, if try the same query with the direct values the query runs very fast. I don't know if there is an answer to this or if it is a bug, but, in many forums the people has the same problem: very slow stored procedures that uses date parameters.

If you use the code that i propose. The SP becomes fast as if you put the direct values. Enjoy.

Total article views: 2812 | Views in the last 30 days: 2
Related Articles

datetime query issue

datetime query issue


DateTime Problem



datetime based query

help mewritingthis datetime based query


Stored procedure slower then query

Stored procedure slower then query


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones