简体   繁体   中英

Get the most recent Friday's date SQL

I'm trying to get the most recent Friday in SQL Server 2008.

I have this. It gets the beginning of the week (monday) then subtracts 3 days to get Friday.

declare @recentFriday datetime =  DATEADD(ww, DATEDIFF(dd,0,GETDATE()), 0)-3

When this is run during the week, it gets last Friday's date which is correct. But when run on Friday (or Saturday), it still gets last week's date instead of the current week's Friday. I'm about to use if/else conditions but I'm sure there's an easier way.

This works for any input and any setting of DATEFIRST :

dateadd(d, -((datepart(weekday, getdate()) + 1 + @@DATEFIRST) % 7), getdate())

It works by adjusting the weekday value so that 0 = Friday, simulating Friday as the beginning of the week. Then subtract the weekday value if non-zero to get the most recent Friday.

Edit : Updated to work for any setting of DATEFIRST .

DECLARE @date DATETIME = '20110512' -- Thursday
SELECT DATEADD(DAY,-(DATEDIFF(DAY,'19000105',@date)%7),@date) --20110506

SET @date = '20110513' -- Friday
SELECT DATEADD(DAY,-(DATEDIFF(DAY,'19000105',@date)%7),@date) --20110513

SET @date = '20110514' -- Saturday
SELECT DATEADD(DAY,-(DATEDIFF(DAY,'19000105',@date)%7),@date) --20110513
  1. Calculate the number of days between a known Friday (05 Jan 1900) and the given date
  2. The remainder left from dividing the difference in 1. by 7 will be the days elapsed since the last Friday
  3. Subtract the remainder in 2. from the given date

you can check if the current day of week is friday or greater DATEPART(dw,GETDATE()) and then call (SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)+4) or (SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)-3)

SELECT 
CASE WHEN DATEPART(dw,GETDATE()) >= 5 THEN 
(SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)+4) 
ELSE 
(SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)-3) 
END

Using a known Friday date (I'll use Jan 7, 2011) as a starting point, you can do this:

DECLARE @d DATETIME

SET @d = '2011-05-13' /* Friday */
SELECT DATEADD(DAY, (DATEDIFF (DAY, '20110107', @d) / 7) * 7, '20110107')
/* Returns 2011-05-13 */

SET @d = '2011-05-12' /* Thursday */
SELECT DATEADD(DAY, (DATEDIFF (DAY, '20110107', @d) / 7) * 7, '20110107')
/* Returns 2011-05-06 */

Just choose a known Friday that's older than any dates you'll be using in your calculations.

SELECT CONVERT(VARCHAR(12),GETDATE()) AS Today,
CASE WHEN (DATEPART(DW,GETDATE())< 7) 
THEN CONVERT(VARCHAR(12),(DATEADD(dd,-(DATEPART(DW,GETDATE())+1),GETDATE())))
ELSE CONVERT(VARCHAR(12),(DATEADD(d,- 1,GETDATE())))
END AS [Last Friday]

The other solutions were not working for my use case.

This works for finding any previous day by replacing 'Sunday' with the day you`re looking for:

    DECLARE @myDate DATE = GETDATE()

    WHILE DATENAME(WEEKDAY, @myDate) <> 'Sunday'
    BEGIN
        SET @myDate = DATEADD(DAY, -1, @myDate)
    END

Here is a completly set oriented way to achive the last Friday:

select Friday from
(
select max(GetDate()) as Friday where datepart(dw, getdate()) = 6
union all
select max((GetDate() - 1)) where datepart(dw, (getdate() - 1)) = 6
union all
select max((GetDate() - 2)) where datepart(dw, (getdate() - 2)) = 6
union all
select max((GetDate() - 3)) where datepart(dw, (getdate() - 3)) = 6
union all
select max((GetDate() - 4)) where datepart(dw, (getdate() - 4)) = 6
union all
select max((GetDate() - 5)) where datepart(dw, (getdate() - 5)) = 6
) x where Friday is not null

This is what I got I hope it helps

DECLARE @UserDate DateTime
SET @UserDate = '2020-09-03'
SELECT DATEADD(day, (6 - datepart(weekday, @UserDate)) , @UserDate)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM