nagarjuna nagarjuna - 2 years ago 71
SQL Question

In sqlserver how to find last before week data?

I am trying the below query, but it's output comes last 2 weeks data, but I need only last before week data only.

My week starts from Friday. So I need all rows between Friday to Friday.

select * from tablename where createddate>=DATEADD(WEEK,-2, GETDATE()) ;

Answer Source

From one your comments I found that the week you are talking about starts from Friday, so you need to add up those gap days into your condition

SELECT * FROM tablename 
WHERE createddate >= DATEADD(ww, DATEDIFF(ww, 4 ,DATEADD(WEEK, -1, GETDATE())), 4)
AND createddate < DATEADD(ww, DATEDIFF(ww, 4 ,DATEADD(WEEK, 0, GETDATE())), 4)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download