How to Find Day Name From Date in SQL Server?
Last Updated :
09 Sep, 2024
Finding the day name from a specific date is a common task in SQL Server, useful for generating reports, analyzing trends, or scheduling. SQL Server provides two primary methods such as the DATENAME()
function and the FORMAT()
function.
In this article, We will learn about How to Find Day Name From the Date in an SQL Server in detail and so on.
How to Find Day Name From Date in an SQL Server?
To find the Day Name From Date in SQL Server we will use the below method for better understanding as defined below:
- Using the DATENAME() Function
- Using FORMAT() Function
1. Using the DATENAME() Function
The DATENAME() function in SQL Server finds a given part of the specified date. Moreover, it returns the output value as a string.
Syntax
Syntax finds the day name from a date using the DATENAME() function:
DATENAME(type, Date)
Example 1:
In this example, we will find the day name from a given date using DATETIME function in SQL Server.
Suppose we want to declare a specific date and retrieve both the date and the day of the week name for that date.
DECLARE @Date DATE = '2020-12-22';
SELECT @Date As [TDate],
DATENAME(WEEKDAY, @Date) AS [Day_Name];
Output:
Explanation: This query declares a variable @Date
of type DATE
with the value '2020-12-22'
. It then selects two columns: the first column returns the value of @Date
as TDate
, and the second column uses the DATENAME(WEEKDAY, @Date)
function to return the name of the day of the week (e.g., Tuesday) for the given date. The WEEKDAY
argument in DATENAME
specifies that the function should return the full name of the day
Example 2:
Query:
Suppose we want to declare a date and retrieve both the date itself and the name of the day (e.g., Monday, Tuesday) corresponding to that date.
DECLARE @Date DATE = '2022-12-22';
SELECT @Date AS [TDate],
DATENAME(w, @Date) AS [Day_Name];
Output:
Explanation: This query declares a variable @Date
of type DATE
with the value '2022-12-22'
. It then selects two columns: the first returns the value of @Date
as TDate
, and the second column returns the name of the day (e.g., Thursday) using the DATENAME(w, @Date)
function, where w
represents the day of the week
2. Using FORMAT() Function
The FORMAT() function is one of the String functions in SQL Server, which is used to format the specified value in the given format. We can use this function to extract and format the date in day name format.
Syntax:
Syntax to find day name from a date using FORMAT function is:
FORMAT(Date, 'dddd')
Example 1:
In this example, we will find the day name from a given date using FORMAT function in SQL Server.
DECLARE @Date DATE = '2021-12-24';
SELECT @Date As [TDate],
FORMAT(@Date, 'dddd') AS [Day_Name]
Output:
Example 2:
Let us suppose we have below table name "GeekLogin":
Step 1: To Create Table
Query:
CREATE TABLE GeekLogin
( Name varchar (22),
ID int, LoginDate date) ;
Step 2: Insert Values in the table.
Query:
INSERT INTO GeekLogin (Name, LoginId, LoginDate)
VALUES
('Khushi', 2, '2019-07-22'),
('Megha', 4, '2019-09-23'),
('Komal', 3, '2019-08-27'),
('Mona', 5, '2019-12-19'),
('Ankit', 7, '2019-09-12'),
('Deepak', 8, '2019-09-04');
Name | ID | LoginDate |
---|
Khushi | 2 | 2019-07-22 |
Megha | 4 | 2019-09-23 |
Komal | 3 | 2019-08-27 |
Mona | 5 | 2019-12-19 |
Ankit | 7 | 2019-09-12 |
Deepak | 8 | 2019-09-04 |
Step 3: To check LoginDay for LoginDate, we could use the below query.
Query:
Suppose we need to retrieve the names, IDs, and login dates of users from the GeekLogin
table, along with the day of the week on which each login occurred.
SELECT TOP (1000) [Name],[ID],[LoginDate],
DATENAME(w, LoginDate) AS [LoginDay]
FROM [GeekLogin];
Output:
Conclusion
In SQL Server, the DATENAME()
and FORMAT()
functions are powerful tools for extracting the day name from a date. The DATENAME()
function offers a straightforward approach for retrieving the full name of the day of the week, while the FORMAT()
function provides flexibility in formatting dates into various string representations.
Similar Reads
How to Remove Times from Dates in SQL Server
In SQL Server, there are Date and DateTime data types to store Date and Time values. There can be situations when only the date value needs to be displayed or to do date calculations for date add, date difference functions, and between two dates from datetime values. So, to remove the Time part from
4 min read
How to Extract Day of Week From Date Field in PostgreSQL?
In PostgreSQL, extracting the day of the week from a date field is a common and essential task when working with temporal data. Knowing the day of the week is crucial for various applications, including scheduling, reporting, and analytical purposes. PostgreSQL provides multiple methods to achieve t
5 min read
How to Group by Day, Date, Hour, Month or Year in SQL Server
Grouping data by day, date, hour, month, or year in SQL Server involves using the GROUP BY clause and appropriate date functions to extract or manipulate the relevant portions of the datetime column. The SQL Server GROUP BY operator is similar to the SQL GROUP BY Operator. The GROUP BY clause is a p
3 min read
How to Exclude Weekend Days in a SQL Server Query?
With this article, we will learn how to exclude weekend days in a SQL server query. For this task, we use the DATEADD ( ) MS.SQL server function. This function in SQL Server is used, to sum up, a time or a date interval to a specified date then returns the modified date. Syntax : DATEADD(interval, n
2 min read
How to Format a Date in PostgreSQL
PostgreSQL provides powerful tools to format dates into different styles and patterns using functions like TO_CHAR(). Formatting dates in PostgreSQL is important when we need to present data in a more readable or application-friendly way. This article will explain how to format a date in PostgreSQL
4 min read
Update Date Field in SQL Server
The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statements as per our requirement. With this article, we will learn how to Update the Date Field in SQL Server. In this article, we w
2 min read
DATEADD() Function in SQL Server
DATEADD() function : This function in SQL Server is used to sum up a time or a date interval to a specified date, then returns the modified date. Features : This function is used to sum up a time or a date interval to a date specified. This function comes under Date Functions. This function accepts
3 min read
How to Compare Time in MS SQL Server?
To compare time in MS SQL Server, use the comparison operators (=,<,>, etc.). In this article, we will be making use of the Microsoft SQL Server as our database. and comparing times using both pre-defined dates and the current date and time with the GETDATE() function.First, let's create a dat
3 min read
How to Create Daily, Weekly, and Monthly Report in SQL Server?
SQL Server is a versatile database and a most used database throughout the world. In this article, let us see SQL queries how to get Daily, Weekly, and Monthly reports from SQL Server. Let us start in creating a database and sample details Step 1: Database creation Command to create the database. He
3 min read
DATENAME() Function in SQL Server
DATENAME() function : This function in SQL Server is used to find a given part of the specified date. Moreover, it returns the output value as a string. Features : This function is used to find a given part of the specified date.This function comes under Date Functions.This function accepts two para
2 min read