- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - DAY() Function
The SQL DAY() function provides an integer value that indicates the day of the month (from 1 to 31) of the given date. The argument must be an expression that returns a value of one of the built-in data types of date, timestamp, character string, or graphic string.
The expression must be a valid string representation of a date or timestamp with an actual length of not more than 255 bytes if it is a character or graphic string, and it must not be a CLOB or DBCLOB.
Syntax
Following is the syntax for the SQL DAY() function −
DAY(date)
Parameters
This function accepts one parameter as discussed below −
date − used to return the da of month from.
Example
In the following example where we are going to mention the invalid string and retriveing the day −
SELECT DAY(2022-11-11);
Output
When we execute the above query, the output is obtained as follows −
+-----------------+ | DAY(2022-11-11) | +-----------------+ | 24 | +-----------------+
Example
Let's look into the following example where we are going to retrieve the day using the following query −
SELECT DAY('');
Output
When we execute the above query, the output is obtained as follows −
+---------+ | DAY('') | +---------+ | NULL | +---------+
Example
Here, we are trying to retrieve the day of month for a date using the following query −
SELECT DAY('2023/02/16') AS DayOfMonth;
Output
When we execute the above query, the output is obtained as follows −
+------------+ | DayOfMonth | +------------+ | 16 | +------------+
Example
Here, we are trying to find the day where only the time part was mentioned using the following query −
SELECT DAY('12:28:06') AS DAY;
Output
On executing the above query, the output is displayed as follows −
+-----+ | DAY | +-----+ | 1 | +-----+
Example
In the following example we are going to retrieve the day using CURRENT_TIMESTAMP execute the below query to do so.
SELECT DAY(CURRENT_TIMESTAMP);
Output
The output for the above query is produced as given below −
+------------------------+ | DAY(CURRENT_TIMESTAMP) | +------------------------+ | 16 | +------------------------+
Example
Look at the following example, where we are using the alternative function FORMAT() to retrieve the date by using the following query −
SELECT FORMAT(CAST('2023-02-16' AS DATE), 'dddd');
Output
If we execute the above query, the result is produced as follows −
+----------+ | DAY | +----------+ | Thursday | +----------+
Example
Considering the following example, where we are using the day() function with a variable and retrieving the day of the month from the specified date by using the following query −
DECLARE @DATE VARCHAR(25); SET @DATE = '2023/02/16'; SELECT DAY(@DATE) AS DAY ;
Output
On executing the above query, it will generate the following output as shown below −
+------+ | DAY | +------+ | 16 | +------+
Example
We can also use column name as an argument to the DAY() function. Let us create a table with the name IPLPlayers in the SQL database using the CREATE statement as shown in the query below −
CREATE TABLE IPLPlayers( ID INT, Name VARCHAR(255), Date_Of_Birth date, Country VARCHAR(255), PRIMARY KEY (ID) );
Now, let us insert some records in the IPLPlayers table using INSERT statements as shown in the query below −
INSERT INTO IPLPlayers VALUES (1, 'Shikhar','1999-11-10','India'); INSERT INTO IPLPlayers VALUES (2, 'Dhoni','1992-12-11','Jharkhand'); INSERT INTO IPLPlayers VALUES (3, 'Gayle','1993-10-12','Westindies'); INSERT INTO IPLPlayers VALUES (4, 'Williamson','1992-09-23','NewZealand');
We can verify whether the table IPLPlayers is created or not using the following query −
SELECT * FROM IPLPlayers;
The table IPLPlayers is successfully created in the SQL database −
+----+------------+---------------+------------+ | ID | Name | Date_Of_Birth | Country | +----+------------+---------------+------------+ | 1 | Shikhar | 1999-11-10 | India | | 2 | Dhoni | 1992-12-11 | Jharkhand | | 3 | Gayle | 1993-10-12 | Westindies | | 4 | Williamson | 1992-09-23 | NewZealand | +----+------------+---------------+------------+
Now we are going to retrieve the day from the Date_Of_Birth column of the table by running the following query −
SELECT Name,Date_Of_Birth, Country, DAY(Date_Of_Birth) FROM IPLPlayers;
Output
When we execute the above query, the output is obtained as follows −
+------------+---------------+------------+--------------------+ | Name | Date_Of_Birth | Country | DAY(Date_Of_Birth) | +------------+---------------+------------+--------------------+ | Shikhar | 1999-11-10 | India | 10 | | Dhoni | 1992-12-11 | Jharkhand | 11 | | Gayle | 1993-10-12 | Westindies | 12 | | Williamson | 1992-09-23 | NewZealand | 23 | +------------+---------------+------------+--------------------+
To Continue Learning Please Login
Login with Google