How do you use the DATENAME function to return the name of a date part (e.g., Monday)?
Posted by HenryPk
Last Updated: July 11, 2024
The DATENAME function in SQL Server is used to return the name of a specified date part for a given date. The syntax for the DATENAME function is:
DATENAME(datepart, date)
- datepart: The part of the date you want to get the name for. This could be year, quarter, month, day of week, etc. - date: The actual date from which you want to extract the name. If you want to return the name of the day for a particular date (e.g., "Monday" for a date that falls on a Monday), you would specify the date part as weekday.
Example Usage:
Here is an example SQL query that uses the DATENAME function to return the name of the day for a specific date:
SELECT DATENAME(weekday, '2023-10-30') AS DayName;
In this example, if October 30, 2023, falls on a Monday, the result will be:
DayName
--------
Monday
You can also use DATENAME with the current date to find out the current day name:
SELECT DATENAME(weekday, GETDATE()) AS CurrentDayName;
This will return the name of the current day of the week based on the system's date.
Related Content