How to Get the Day Name From a Date using SQL Server

A lot of users have confusions over how to get a Day Name from a Date in SQL Server. I have seen developers writing custom functions to achieve this simple requirement. The solution lies in the DATENAME function

I will take an example of the Northwind database;

SELECT OrderID, CustomerID, OrderDate,
DATENAME(dw, OrderDate) as DayName
From Orders

OUTPUT

image

Observe how we are using the DATENAME function specifying ‘dw’ or ‘weekday’ as the datepart. Simple, wasn’t it!

It helps to check the SQL Server Books Online to go through the built-in functions. It will benefit you in the long run!

No comments:

Post a Comment