Syntax
DATANAME(interal, date)
The function takes two parameters (or arguments).
• interval: The specified part of a date argument that this function will return. It can be "year", "month", "day", "week" etc. This is required.
• date: The date to use.
Example
Let us assume, I have a table named "dbo.sales", which has four columns. See the last column in the below image. Its a datetime column.
I want to get distinct months, in words, from the table. I can use the DATENAME() function to the get what I want.
Here's the SQL query.
SELECT DISTINCT DATENAME(MONTH, SalesDate) Months FROM Sales
Here's the result.
Using DATENAME() in WHERE Clause
You can use the DATENAME() function inside the WHERE clause. For example, I want to extract records for the month of April. Here's the query.
SELECT * FROM dbo.Sales WHERE DATENAME(MONTH, SalesDate) = 'APRIL'