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
 General SQL Server Forums
 Script Library
 Week = Friday to Thursday

Author  Topic 

cidr
Posting Yak Master

207 Posts

Posted - 2014-10-20 : 08:00:21
Hi There,

At the moment I'm trying to develop a report that will return data from the most recent Friday. In this case the 17th Oct.

The last day of the week will be Thursday coming (23 Oct). Then on This Friday (24th oct) and new week starts and data will be only displayed from that day... and so on.

Does anyone have any ideas?

Thanks in advance.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-20 : 09:54:18
that's pretty vague. Start with building SQL queries to return the rowsets your report will need. You can use SQL functions like dateadd and datediff to get the ranges you want
Go to Top of Page

cidr
Posting Yak Master

207 Posts

Posted - 2014-10-20 : 11:09:54
Thanks for responding; it was just to get ideas of how some would perform this.

In the end, I just done like so:


DECLARE @StartOfWeek datetime =
CASE
WHEN DATEPART(DW,GETDATE()) = 6 THEN GETDATE()
WHEN DATEPART(DW,GETDATE()) = 5 THEN GETDATE()-6
WHEN DATEPART(DW,GETDATE()) = 4 THEN GETDATE()-5
WHEN DATEPART(DW,GETDATE()) = 3 THEN GETDATE()-4
WHEN DATEPART(DW,GETDATE()) = 2 THEN GETDATE()-3
WHEN DATEPART(DW,GETDATE()) = 1 THEN GETDATE()-2
WHEN DATEPART(DW,GETDATE()) = 7 THEN GETDATE()-1
END
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-10-26 : 06:37:12
[code]WITH cteSampleData(theDate)
AS (
SELECT DATEADD(DAY, number, '20140101') AS theDate
FROM master.dbo.spt_values
WHERE type = N'P'
AND Number < 300
)
SELECT theDate,
DATEADD(DAY, DATEDIFF(DAY, '18991229', theDate) / 7 * 7, '18991229') AS StartDateOfCurrentWeek,
DATEADD(DAY, DATEDIFF(DAY, '18991222', theDate) / 7 * 7, '18991229') AS StartDateOfNextWeek
FROM cteSampleData;

/*
SELECT *
FROM dbo.MyFactTable
WHERE MyDateTimeColumn >= @StartDateOfCurrentWeek
AND MyDateTimeColumn < @StartDateOfNextWeek
*/[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-10-26 : 14:59:19
quote:
Originally posted by cidr

Thanks for responding; it was just to get ideas of how some would perform this.

In the end, I just done like so:


DECLARE @StartOfWeek datetime =
CASE
WHEN DATEPART(DW,GETDATE()) = 6 THEN GETDATE()
WHEN DATEPART(DW,GETDATE()) = 5 THEN GETDATE()-6
WHEN DATEPART(DW,GETDATE()) = 4 THEN GETDATE()-5
WHEN DATEPART(DW,GETDATE()) = 3 THEN GETDATE()-4
WHEN DATEPART(DW,GETDATE()) = 2 THEN GETDATE()-3
WHEN DATEPART(DW,GETDATE()) = 1 THEN GETDATE()-2
WHEN DATEPART(DW,GETDATE()) = 7 THEN GETDATE()-1
END



make sure you read this and then go through SwePeso's suggestion

http://visakhm.blogspot.in/2012/08/creating-server-independent-day.html



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -