LASTWEEKDAY
Updated 2023-10-06 23:57:34.147000
Syntax
SELECT [westclintech].[wct].[LASTWEEKDAY](
<@StartDate, datetime,>
,<@DayofWeek, nvarchar(4000),>)
Description
Use the scalar function LASTWEEKDAY to return the last specified day of the week in any calendar month.
Arguments
@DayofWeek
the day of the week to be returned. @DayofWeek can be specified using the numbers 1 – 7, where 1 is Sunday and 7 is Saturday, or by using the name of the day of the week (e.g. 'Monday') or the abbreviated name of the week (e.g. 'Mon') as controlled by the Windows Regional Settings on the Windows Server.
@StartDate
specifies the month and year of the return value. LASTWEEKDAY returns the first @DayofWeek in the month and year of @StartDate. @StartDate is an expression of type datetime or of a type that can be implicitly converted to datetime.
Return Type
float
Remarks
@StartDate must be a valid date.
@StartDate only determines the month and year of the returned value.
@DayofWeek always understands 1 to mean Sunday and 7 to mean Saturday. The day of week name and abbreviated name are validated against the Windows Server Regional Settings and not the SQL Server language settings.
Use the FIRSTWEEKDAY function to calculate the first specified day of the week in any calendar month.
Examples
In this example we calculate the last Wednesday in January 2013.
SELECT wct.LASTWEEKDAY('2013-01-01', 4) as [Last Wednesday];
This produces the following result.
{"columns":[{"field":"Last Wednesday","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Last Wednesday":"2013-01-30 00:00:00.000"}]}
In this example, we calculate the Friday before the last Wednesday of the month.
SELECT DATEADD(d, -5, wct.LASTWEEKDAY('2013-01-01', 'Wednesday')) as [Friday];
This produces the following result.
{"columns":[{"field":"Friday","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Friday":"2013-01-25 00:00:00.000"}]}
See Also
BUSDAYS - Calculate the number of business days between two dates
BUSINESSDATE - Calculate a Business Date from an offset
BUSINESSDATEWE - Calculate a Business Date from an offset and specified weekend days
FIRSTWEEKDAY - Calculate the last occurrence of a weekday in a given month
NETWORKDAYS - Calculate the number of working days between two dates (inclusive)
YEARFRAC - Calculate the difference between two dates as a fraction of a year