give me 15 mins to try it out. thanks :-)
Main Topics
Browse All TopicsHello,
i have to select information for yesterday, today, tomorrow,
week to date, last week, month to date, last month
I have the basic SQL but am getting a bit lost in allowing for last week being in a different year to this; same for the month.
ie if I am running the sql on the 1st January 2004, last month is dec 2003
My SQL is below. Am I approaching it all wrong or does it need a small tweak? A m I better to sue BEWTEEN and calc values for end of last week, start of last week, start of this week, end of last month, start of last month (using a full dd-mm-yyyy value)?
Thanks,
Mark
SELECT @yesterday = COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE CONVERT(CHAR(12),StartDate
SELECT @today = COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE CONVERT(CHAR(12),StartDate
SELECT @tomorrow = COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE CONVERT(CHAR(12),StartDate
-- this week
SELECT startDate ,DatePart(wk,getdate()-7) ,StartDate,
DatePart(wk,getdate())
FROM MS_RBM..Bookings rbm
WHERE DatePart(wk,StartDate) = DatePart(wk,getdate())
--last week
SELECT startDate ,DatePart(wk,getdate()-7) ,StartDate,
DatePart(wk,getdate())
FROM MS_RBM..Bookings rbm
WHERE DatePart(wk,StartDate) = DatePart(wk,getdate()-7)
?? year
--this month
SELECT startDate ,DatePart(m,getdate()),Dat
DatePart(m,getdate())
FROM MS_RBM..Bookings rbm
WHERE DatePart(m,StartDate) = DatePart(m,getdate())
AND DatePart(yy,StartDate) = DatePart(yy,getdate())
--last month
SELECT startDate ,DatePart(m,dateadd(month,
FROM MS_RBM..Bookings rbm
WHERE DatePart(m,StartDate) = DatePart(m,dateadd(month,-
-- year??
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.
Mark
Days of the week are fairly easy.
Please be aware that the function datediff and datepart will be effected by which day is set as the start of the week. Sunday is the default but it can be changed using SET DATEFIRST
declare @today datetime
set @today = getdate()
SELECT @thismonth= COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(mm,@Today,StartDa
and datepart(dw,StartDate) between 2 and 6
For the public holidays.
You could access the information from the web and integrate it into your database doing a web query, however you would need to find a reliable source for the query.
If the choice was mine I would gather the information once and populate a permenant database table with it. The table may look like
create table PublicHoliday
(
CalendarDate smalldatetime primary key,
Description varchar(50)
)
your query would then be
SELECT @thismonth= COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(mm,@Today,StartDa
and datepart(dw,StartDate) between 2 and 6
and cast(convert(varchar(11),S
I know this is not exactly what you asked for but should provide a satisfactory result as long as you are able to add the PublicHoliday table to your database.
Cheers Sash
Hello,
I had to get some code going yesterday so opened another Q:
http://www.experts-exchang
Business Accounts
Answer for Membership
by: SashPPosted on 2004-02-23 at 03:34:49ID: 10431311
Hi Mark
,103) = CONVERT(CHAR(12),dbo.mDate (getdate() , -1),103)
te) = -1
te) = 0
te) = 1
te) = 0
te) = -1
te) = 0
te) = -1
Firstly you may speed up your SQL by removing some of the conversions from the code.
SELECT @yesterday = COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE CONVERT(CHAR(12),StartDate
Will should execute faster as
declare @Today datetime
set @Today = getdate()
SELECT @yesterday = COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(dd,@Today,StartDa
You can put the getdate() in the SELECT statement but it is good practice to set your constants outside
Now the next couple of commands follow on
SELECT @today= COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(dd,@Today,StartDa
SELECT @tomorrow= COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(dd,@Today,StartDa
The last week is similar
SELECT @thisweek = COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(wk,@Today,StartDa
Last Week
SELECT @lastweek= COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(wk,@Today,StartDa
This month
SELECT @thismonth= COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(mm,@Today,StartDa
Last Month
SELECT @lastmonth= COUNT(*)
FROM MS_RBM..Bookings rbm
WHERE datediff(mm,@Today,StartDa
The function datediff will remove the necessity of working out the month or year.
Sash