LASTWEEKDAY
Updated: 21 December 2012
Use the scalar function LASTWEEEKDAY to return the last specified day of the week in any calendar month.
Syntax
SELECT [wctFinancial].[wct].[LASTWEEKDAY](
<@StartDate, datetime,>
,<@DayofWeek, nvarchar(4000),>)
Arguments
@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.
@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.
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.
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.
Friday
-----------------------
2013-01-25 00:00:00.000
See Also