- 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 - DATENAME() Function
The SQL DATENAME() function is an in-built function in SQL that is used to extract and return a particular segment (as a character string) of a date or time value, such as day of the week, month, year, etc. This function returns an error if the arguments passed to this function are invalid.
To use this function, we need to provide two parameters: the part of the date/time we want to extract (such as the year, month, or day), and the actual date/time value we want to extract it from.
Syntax
Following is the syntax of the SQL DATENAME() function −
DATENAME(datepart, date)
Parameters
This function accepts two parameters. The same is described below −
- datepart − It specifies the date or datetime segment that we want to retrieve. The following are the possible values −
- year, yyyy, yy = Year
- quarter, qq, q = Quarter
- month, mm, m = Month
- dayofyear, dy, y = Day of the year
- day, dd, d = Day
- week, ww, wk = Week
- weekday, dw, w = Weekday
- hour, hh = Hour
- minute, mi, n = Minute
- second, ss, s = Second
- millisecond, ms = Millisecond
- date − It specifies the date or datetime you want to extract a particular segment from.
Example
In the following example, we are trying to retrieve the Year segment from the specified date value using the following query −
SQL> SELECT DATENAME(YEAR, '2023/02/16') AS DATE_IN_YEARS;
Output
On executing the above query, the output is displayed as follows −
+---------------+ | DATE_IN_YEARS | +---------------+ | 2023 | +---------------+
Example
We can retrieve the Month from the specified date value using the following query −
SQL> SELECT DATENAME(MONTH, '2023/02/16') AS DATE_IN_MONTH;
Output
When we execute the above query, the output is obtained as follows −
+---------------+ | DATE_IN_MONTH | +---------------+ | FEBRAURY | +---------------+
Example
Here, we are passing the CURRENT_TIMESTAMP function as a parameter to the DATENAME() function and retrieve the day segment using the following query −
SQL> SELECT DATENAME(DAY, CURRENT_TIMESTAMP) AS DATE_IN_DAY;
Output
If we execute the above query, the result is produced as follows −
+-------------+ | DATE_IN_DAY | +-------------+ | 16 | +-------------+
Example
In this example, we are trying to retrieve the hour segment from the specified datetime value −
SQL> SELECT DATENAME(HOUR, '2023/02/16 10:15:20') AS TIME_IN_HOURS;
Output
If we execute the above query, the result is produced as follows −
+---------------+ | TIME_IN_HOURS | +---------------+ | 10 | +---------------+
Example
In this example, we are trying to retrieve the second segment from the specified datetime value −
SQL> SELECT DATENAME(SECOND, '2023/02/16 10:15:20') AS TIME_IN_SECONDS;
Output
When we execute the above query, the output is obtained as follows −
+-----------------+ | TIME_IN_SECONDS | +-----------------+ | 20 | +-----------------+
Example
If we try to pass invalid values as parameters to the function, it returns an error statement.
SQL> SELECT DATENAME(MONTH, '2023/14/25 10:34:12') AS DATE_IN_MONTH;
Error
If we execute the above query, the result is produced as follows −
Conversion failed when converting date and/or time from character string.
Example
Assume we have created a table with the name EMPLOYEE in the SQL database using the CREATE statement as shown in the query below −
SQL> SELECT ID, NAME, DATE_OF_BIRTH, DATENAME(YEAR, DATE_OF_BIRTH) AS DOB_IN_YEARS FROM EMPLOYEE;
Now, let us insert some records in the EMPLOYEE table using INSERT statements as shown in the query below −
SQL> INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(1, 'Dhruv', '2001-12-05'); INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(2, 'Arjun', '2009-03-01'); INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(3, 'Dev', '2014-03-15'); INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(4, 'Riya', '2005-12-05'); INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(5, 'Aarohi', '2003-05-02'); INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(6, 'Lisa', '2002-11-25'); INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(7, 'Roy', '2012-05-30');
We can verify whether the table is created or not using the following query −
SQL> SELECT * FROM EMPLOYEE
The table EMPLOYEE is successfully created in the SQL database.
+-----+--------+---------------+ | ID | NAME | DATE_OF_BIRTH | +-----+--------+---------------+ | 1 | Dhruv | 2001-12-05 | | 2 | Arjun | 2009-03-01 | | 3 | Dev | 2014-03-15 | | 4 | Riya | 2005-12-05 | | 5 | Aarohi | 2003-05-02 | | 6 | Lisa | 2002-11-25 | | 7 | Roy | 2012-05-30 | +-----+--------+---------------+
Here, we are retrieving the YEARS from the employee date of birth using the following query −
SQL> SELECT ID, NAME, DATE_OF_BIRTH, DATENAME(YEAR, DATE_OF_BIRTH) AS DOB_IN_YEARS FROM EMPLOYEE
Output
When we execute the above query, the output is obtained as follows −
+-----+---------+---------------+--------------+ | ID | NAME | DATE_OF_BIRTH | DOB_IN_YEARS | +-----+---------+---------------+--------------+ | 1 | Dhruv | 2001-12-05 | 2001 | | 2 | Arjun | 2009-03-01 | 2009 | | 3 | Dev | 2014-03-15 | 2014 | | 4 | Riya | 2005-12-05 | 2005 | | 5 | Aarohi | 2003-05-02 | 2003 | | 6 | Lisa | 2002-11-25 | 2002 | | 7 | Roy | 2012-05-30 | 2012 | +-----+---------+---------------+--------------+
Example
Let us create another table with the name SCREENTIME in the SQL database using the CREATE statement as shown in the query below −
SQL> CREATE TABLE SCREENTIME(ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, WATCHING_TIME VARCHAR (20));
Now, let us insert some records in the SCREENTIME table using INSERT statements as shown in the query below −
SQL> INSERT INTO SCREENTIME(ID, NAME, WATCHING_TIME) VALUES(1, 'Dhruv', '12:12:27'); INSERT INTO SCREENTIME(ID, NAME, WATCHING_TIME) VALUES(2, 'Arjun', '15:11:34'); INSERT INTO SCREENTIME(ID, NAME, WATCHING_TIME) VALUES(3, 'Dev', '04:45:56'); INSERT INTO SCREENTIME(ID, NAME, WATCHING_TIME) VALUES(4, 'Riya', '05:12:23'); INSERT INTO SCREENTIME(ID, NAME, WATCHING_TIME) VALUES(5, 'Aarohi', '19:43:21'); INSERT INTO SCREENTIME(ID, NAME, WATCHING_TIME) VALUES(6, 'Lisa', '23:12:23'); INSERT INTO SCREENTIME(ID, NAME, WATCHING_TIME) VALUES(7, 'Roy', '05:33:00');
We can verify whether the table is created or not using the following query −
SQL> SELECT * FROM SCREENTIME
The table SCREENTIME is successfully created in the SQL database.
+-----+--------+----------------+ | ID | NAME | WATCHING_TIME | +-----+--------+----------------+ | 1 | Dhruv | 12:12:27 | | 2 | Arjun | 15:11:34 | | 3 | Dev | 04:45:56 | | 4 | Riya | 05:12:23 | | 5 | Aarohi | 19:43:21 | | 6 | Lisa | 23:12:23 | | 7 | Roy | 05:33:00 | +-----+--------+----------------+
Here, we are trying to retrieve the watching time of people in HOURS using the following query −
SQL> SELECT ID, NAME, WATCHING_TIME, DATENAME(HOUR, WATCHING_TIME) AS WATCHING_HOURS FROM SCREENTIME;
Output
The output for the above query is produced as given below −
+-----+--------+----------------+----------------+ | ID | NAME | WATCHING_TIME | WATCHING_HOURS | +-----+--------+----------------+----------------+ | 1 | Dhruv | 12:12:27 | 12 | | 2 | Arjun | 15:11:34 | 15 | | 3 | Dev | 04:45:56 | 4 | | 4 | Riya | 05:12:23 | 5 | | 5 | Aarohi | 19:43:21 | 19 | | 6 | Lisa | 23:12:23 | 23 | | 7 | Roy | 05:33:00 | 5 | +-----+--------+----------------+----------------+
To Continue Learning Please Login
Login with Google