write t-sql code to find the last business day of the month
- User
write t-sql code to find the last business day of the month
Assistant
To find the last business day of the month in T-SQL, you can use the following code:
vbnetUSE AdventureWorks2017 GO DECLARE @dt DATETIME DECLARE @dtEndOfMonth DATETIME SET @dt = DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) SET @dtEndOfMonth = DATEADD(day, -1, DATEADD(month, 1, @dt)) IF DATEPART(dw, @dtEndOfMonth) = 1 BEGIN SET @dt = DATEADD(day, 1, @dtEndOfMonth) END SELECT @dtThis code uses the
DATEPARTfunction to extract the day of the month from the@dtvariable and then checks if the day of month is 1. If it is, the code then subtracts one day using theDATEADDfunction and theDATEPARTfunction to get the last business day of the month. If the day of month is not 1, the code increments the day of the month by 1 and repeats the process until the day of month is 1. The code then assigns the last business day of the month to the@dtEndOfMonthvariable.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.