SQL Server DATENAME() Function - Get the name of distinct Months in words from a date column

← Prev

In this article I am going to show you how to get distinct months, in words, from a date column using DATENAME() function in SQL Server. The DATENAME() is an SQL Server built-in function that returns a specified part (a datepart) of a specified date.

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.

Get the name of distinct Months in words from a date 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.

sql server dataname function example

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'

using dataname function inside where clause

← Previous