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              |
+-----+--------+----------------+----------------+
sql-date-functions.htm
Advertisements