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 using T-SQL, you can use the
EOMONTHfunction to get the last day of the month and then adjust the date based on the day of the week. Here's an example code snippet:DECLARE @date DATE = GETDATE(); -- Replace with your desired date SELECT CASE WHEN DATENAME(WEEKDAY, EOMONTH(@date)) = 'Saturday' THEN EOMONTH(@date) - 1 WHEN DATENAME(WEEKDAY, EOMONTH(@date)) = 'Sunday' THEN EOMONTH(@date) - 2 ELSE EOMONTH(@date) END AS LastBusinessDay;Explanation:
-
The
GETDATE()function is used to get the current date. You can replace it with any desired date for which you want to find the last business day of the month. -
The
EOMONTH(@date)function returns the last day of the month for the given date.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.