SQL DATENAME()
The DATENAME function in SQL is used to retrieve a specific part of a date, such as the year, month, day, or weekday. It returns the result as a string. This function is particularly useful when you need to format or extract meaningful date information for reporting and analysis.
In this tutorial, we will explore the DATENAME function, its syntax, and practical examples to demonstrate its usage.
Syntax of SQL DATENAME Function
The basic syntax of the DATENAME function is as follows:
</>
Copy
DATENAME(datepart, date)
Where:
datepart: Specifies the part of the date to extract (e.g., year, month, weekday).date: The date value from which the information will be retrieved.
List of Date Parts in SQL DATENAME
| Date Part | Description | Example Output |
|---|---|---|
year | Returns the year | 2025 |
month | Returns the full name of the month | February |
day | Returns the day of the month | 12 |
weekday | Returns the full name of the weekday | Tuesday |
hour | Returns the hour | 14 |
minute | Returns the minute | 30 |
second | Returns the second | 45 |
Step-by-Step Examples Using SQL DATENAME
1 Extracting the Month and Weekday from a Date
Let’s retrieve the month and weekday from a given date:
</>
Copy
SELECT
DATENAME(month, '2025-02-12') AS Month_Name,
DATENAME(weekday, '2025-02-12') AS Weekday_Name;
