In today’s blog post we will a very simple script where we will find day name from any valid date. There are multiple ways you can do it and we will see it in multiple ways.
In our example, we will be using the date of today which is July 27, 2020, and it is Monday today.
Method 1: DateName() Function for Day Name
This is one of the most popular methods.
DECLARE @DateVal DATE = '2020-07-27'; SELECT @DateVal As [Date], DATENAME(WEEKDAY, @DateVal) AS [Day Name];
When you run the above script you will see the date which is passed along with the day name.
Instead of weekday you can also use the abbreviation for it which is dw or w and it will give you the same result.
DECLARE @DateVal DATE = '2020-07-27'; SELECT @DateVal As [Date], DATENAME(WEEKDAY, @DateVal) AS [Day Name], DATENAME(DW, @DateVal) AS [Day Name], DATENAME(W, @DateVal) AS [Day Name];
Another method is to use the format function.
Method 2: FORMAT Function
Here is how you can use the format functions to get the date name from the date.
DECLARE @DateVal DATE = '2020-07-27'; SELECT @DateVal As [Date], FORMAT(@DateVal, 'dddd') AS [Day Name]
The query above will give us the day name from the date.
Here are a few additional blog posts which are related to this topic:
- SQL SERVER – Building Date and Time with DATETIMEFROMPARTS()
- SQL SERVER – Date and Time Functions – EOMONTH() – A Quick Introduction
- SQL SERVER – Adding Datetime and Time Values Using Variables
Well, that’s it for today. If you want me to blog about any trick or trick, just leave a comment and I will be happy to blog about it with due credit to you. You can also connect with me via LinkedIn.
Reference:Â Pinal Dave (https://blog.sqlauthority.com)