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 2012 Forums
 Transact-SQL (2012)
 date fonction

Author  Topic 

wided
Posting Yak Master

218 Posts

Posted - 2015-01-16 : 04:12:32

helloo

I have 2 dates (period)

I need to extract the last day of each week in this period


thanks

nagino
Yak Posting Veteran

75 Posts

Posted - 2015-01-16 : 04:49:25
[code]DECLARE @From DateTime = '2015-01-10';
DECLARE @To DateTime = '2015-01-25';

SET DATEFIRST 6; --if 'last day of week' means saturday, set 6.

WITH dates ([day], [week]) AS (
SELECT @From, DATEPART(ww, @From) [week]
UNION ALL
SELECT DATEADD(d, 1, [day]), DATEPART(ww, [day]) [week]
FROM dates
WHERE DATEADD(d, 1, [day]) <= @To
)
SELECT MAX([day])
FROM dates
GROUP BY [week];[/code]

-------------------------------------
From Japan
Sorry, my English ability is limited.
Go to Top of Page

wided
Posting Yak Master

218 Posts

Posted - 2015-01-16 : 05:46:20
thanks nagino

it's ok
Go to Top of Page
   

- Advertisement -