Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i get dates for last weeks information

Status
Not open for further replies.

MattRichardsUK

Technical User
Dec 11, 2000
22
0
0
GB
I have a table i need to query the table holds in formation for the last year my only needs to see the infor for the last full week, eg if today is Tuesday i would need the information for last week eg the previous Sunday to Saturday.
do you have any ideas?
 
Try use this Function

DATEDIFF (T-SQL) Returns the number of date and time boundaries crossed between two specified dates.

Syntax
DATEDIFF(datepart, startdate, enddate)

Datepart Abbreviations
week wk, ww
 
Or....

select
from
where datetime >= convert(char(8), getdate()-7, 1)
and datetime < convert(char(8), getdate(), 1)

-SQLBill
 
Try this code.

--Declare some variables
Declare
@dw int,
@sun smalldatetime,
@sat smalldatetime

--Get current day of week
Select @dw=datepart(dw,getdate())

--If Saturday (7) set to 0
If @dw=7 Select @dw=0

--Calculate Sunday date
Select @sun=dateadd(d, -6-@dw, convert(char(10), getdate(), 101))

--Calculate Saturday date
Select @sat=@sun+6

--Optional display resulting dates
--Select Sun=@sun, Sat=@sat

--Select rows where date falls between dates calculated
Select * From Table_name
Where DateCol Between @sun and @sat

Note: If DateCol contains time as wel as date values, you'll need to add 1 to the calculated Saturday date. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top