September 23, 2009 at 10:52 am
Hi experts, I have always wanted to know this,
What is the best way to write a tsql code for parametrized date ranges? For example
create procedure procname
(
@datestart datetime,
@dateend datetime
)
as
select column1
from myTable
where date between @datestart and @dateend
I have no ddl or anything, I have just always wanted to know an optimal way to achieve this to prevent problems like excessive reads or parameter sniffing when the date range increase. For the parameter sniffing problem, could declaring the @datestart and @dateend locally work?
September 23, 2009 at 9:28 pm
The data range error, depends on the type of the date we have stored
1)Date only.
2)Date with time.
For all situations, I preferred the method like
Select column1
from myTable
where date >= isnull(@datestart,date)
and date = isnull(@datestart,date)
and date <= isnull(@dateend,date)
September 24, 2009 at 1:19 pm
The only problem with using BETWEEN is that it evaluates to >= and = and = '9/1/2009' AND
theDate < '10/1/2009'
[/code]
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
September 24, 2009 at 1:49 pm
Thanks for the introspective view guys, what about performance? How do you approach that if you were to select all records for 3 months and then 9 months and then a year. Asides from indexing, what ways would you suggest to approach that??
September 24, 2009 at 2:04 pm
After I submitted my last post I noticed you were really asking for performance. Really the only thing you can do is index and update stats. If your data distribution is skewed you may want to look at using OPTIMIZE FOR or PLAN GUIDES to try to get the plan that works best for the majority of queries.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
September 24, 2009 at 5:44 pm
Thank you for your help Jack, both replies helped a lot !!
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply