Great! The stored procedure should be fine.
I don't see :
Thismonth in SP, can you show me how to do thismonth and LastWeek as well (IF POSSIBLE)?
Main Topics
Browse All TopicsSQL experts:
I have a script that allows users to select a date range of :
Today
Yesterday
This Week
This month
Last Month
Last Year
I need to know how to write the query to create a start and end date for each selection above.
Is it possible to do in a query or would you need to do it in say Coldfusion first then use a variable??
I can accept a coldfusion answer or straight sql.
God Bless ALL..
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
you could do just the same with a function
0: today
1: yesterday
2: begin of this week
3: end of this week
4: ...
create function selectdate(@p1 int) returns date
as
begin
declare @x DATE
select @x = case :p1
when 0 then getdate()
when 1 then getdate() -1
when 2 then getdate()-datepart(dw,getd
when 3 then getdate()-datepart(dw,getd
-- etc
end
return (@x)
end
then you can use this in a query
select * from table where date between selectdate(2) and selectdate(3)
Thanks all?
RiteshShaw: With this script would the "last week" and "this week" consider the start of the week?
Like if today is wednesday and they select last week they will need to be returned Monday to Sunday or even Sunday to Saturday of last week would work.
Does the Last week and this week takwe that in consideration?
glad to help!!!
Ritesh Shah
www.SQLHub.com
Business Accounts
Answer for Membership
by: rizwanidreesPosted on 2009-07-04 at 00:21:49ID: 24776160
Option - A, Create a store procedure as follow
m, -1, GETDATE())) -1, GETDATE()))
CREATE PROCEDURE ProcedureName
@p1 int
AS
BEGIN
SET NOCOUNT ON;
if @p1 = 0
-- Today
SELECT * FROM MyTable WHERE DATE=GETDATE()
ELSE if @p1 = 1
-- Yesterday
SELECT * FROM MyTable WHERE DATE=GETDATE()-1
ELSE IF @p1 = 2
-- This Week
SELECT * FROM MyTable WHERE DATE BETWEEN GETDATE()-(DATEPART(dw, GETDATE())-1) AND GETDATE()+(7-DATEPART(dw, GETDATE()))
ELSE IF @p1 = 3
-- Last Month
SELECT * FROM MyTable WHERE MONTH(DATE)=MONTH(DATEADD(
ELSE IF @p1 = 4
-- Last Year
SELECT * FROM MyTable WHERE Year(DATE)=Year(DATEADD(y,
END
Option - B, Don't create store procedure, use coldfusion to decide which query to run