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 PartDescriptionExample Output
yearReturns the year2025
monthReturns the full name of the monthFebruary
dayReturns the day of the month12
weekdayReturns the full name of the weekdayTuesday
hourReturns the hour14
minuteReturns the minute30
secondReturns the second45

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;