Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query to get last week

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-02-12 : 21:54:07
Narayan writes "Hi,

I need SQL query to get the last week's data

I am using the following condition

select convert(datetime, convert(nvarchar,datepart(yyyy,getdate()))
+ '-' + convert(nvarchar,datepart(mm,getdate()))+ '-' + convert(nvarchar,datepart(dd,getdate()-7)))

but if current system date is 3 and if i do -7 it throws up error. How do I get last week date by using current sys date

Regards,
Narayan"

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-12 : 21:57:23
[code]
select [Begin of Last Week] = dateadd(week, datediff(week, 0, getdate()), -7),
[Begin of This Week] = dateadd(week, datediff(week, 0, getdate()), 0)

SELECT . . .
FROM . . .
WHERE datecol >= dateadd(week, datediff(week, 0, getdate()), -7)
AND datecol < dateadd(week, datediff(week, 0, getdate()), 0)
[/code]



KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-13 : 01:00:56
Please define what you consider a week is.

Monday-Sunday?
Sunday-Saturday?

What happens over new year? When is new year?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -