SQL - DATEPART Function



The SQL DATEPART() function is an in-built function that is used to retrieve the specific segment (as an integer) from the date value such as (year, month, or day) or time value such as(hour, minute, or second). 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 you want to extract it from.

Syntax

Following is the syntax of the SQL DATEPART() function −

DATEPART(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 −

SQL> SELECT DATEPART(YEAR, '2023/03/16') AS DATE_IN_YEAR;

Output

When we execute the above query, the output is obtained as follows −

+--------------+
| DATE_IN_YEAR |
+--------------+
| 2023         |
+--------------+

Example

We can retrieve the day segment from the specified date value using the following query −

SQL> SELECT DATEPART(DAY, '2023/03/16') AS DATE_IN_DAY;

Output

On executing the above query, the output is displayed as follows −

+-------------+
| DATE_IN_DAY |
+-------------+
| 16          |
+-------------+

Example

Here, we are trying to retrieve the minute segment from the specified datetime value using the following query −

SQL> SELECT DATEPART(MINUTE, '2023/03/16 18:30:24') AS TIME_IN_MINUTES;

Output

The output for the above query is produced as given below −

+-----------------+
| DATE_IN_MINUTES |
+-----------------+
| 30              |
+-----------------+

Example

We can retrieve the second segment from the specified time datevalue using the following query −

SQL> SELECT DATEPART(SECOND, '2023/03/16 23:12:24') AS TIME_IN_SECONDS;

Output

Let us run the above query, to produce the following result −

+-----------------+
| DATE_IN_SECONDS |
+-----------------+
| 24              |
+-----------------+

Example

Here, we are trying to pass the CURRENT_TIMESTAMP function as a parameter to the DATEPART() function and retrieve the hour segment using the following query −

SQL> SELECT DATEPART(HOUR, CURRENT_TIMESTAMP) AS TIME_IN_HOUR;

Output

When we execute the above query, the output is obtained as follows −

+--------------+
| DATE_IN_HOUR |
+--------------+
| 16           |
+--------------+

Example

Assume we have created a table with the name STUDENTS in the SQL database using the CREATE statement as shown in the query below −

SQL> CREATE TABLE STUDENTS(ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, SCREEN_TIME VARCHAR (20));

Now, let us insert some records in the STUDENTS table using INSERT statements as shown in the query below −

SQL> INSERT INTO STUDENTS(ID, NAME, SCREEN_TIME) VALUES(1, 'Dhruv', '12:12:27');
INSERT INTO STUDENTS(ID, NAME, SCREEN_TIME) VALUES(2, 'Arjun', '15:11:34');
INSERT INTO STUDENTS(ID, NAME, SCREEN_TIME) VALUES(3, 'Dev', '04:45:56');
INSERT INTO STUDENTS(ID, NAME, SCREEN_TIME) VALUES(4, 'Riya', '05:12:23');
INSERT INTO STUDENTS(ID, NAME, SCREEN_TIME) VALUES(5, 'Aarohi', '19:43:21');
INSERT INTO STUDENTS(ID, NAME, SCREEN_TIME) VALUES(6, 'Lisa', '23:12:23');
INSERT INTO STUDENTS(ID, NAME, SCREEN_TIME) VALUES(7, 'Roy', '05:33:00');

We can verify whether the table is created or not using the following query −

SQL> SELECT * FROM STUDENTS

The table STUDENTS is successfully created in the SQL database.

+-----+--------+-------------+
| ID  | NAME   | SCREEN_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 students in HOURS using the following query −

SQL> SELECT ID, NAME, SCREEN_TIME, DATENAME(HOUR, SCREEN_TIME) AS WATCHING_HOURS FROM STUDENTS;

Output

The output for the above query is produced as given below −

+-----+--------+-------------+----------------+
| ID  | NAME   | SCREEN_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              |
+-----+--------+-------------+----------------+

Example

Let us create another 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    |
+-----+--------+---------------+

We can retrieve the YEARS from the employee’s 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         |
+-----+--------+---------------+--------------+

DATENAME() VS DATEPART()

The DATENAME() and DATEPART() functions are used to retrieve the specific segments of a date or datetime values.

The main difference between the DATENAME() and DATEPART() functions is that the DATENAME() function returns the name of the specified date or datetime part as a string, whereas the DATEPART() function returns an integer value that represents the specified date or datetime segment.

Let’s look into the examples below to understand the difference between these two functions.

Example

In the example below, we are trying to retrieve the Month segment from the specified date using the DATENAME() function −

SQL> SELECT DATENAME(MONTH, '2022-12-10') AS DATE_IN_MONTH;

Output

When we execute the above query, the DATENAME() function will return the value as DECEMBER (string value) −

+---------------+
| DATE_IN_MONTH |
+---------------+
| DECEMBER      |
+---------------+

Example

In the example below, we are trying to retrieve the Month segment from the specified date using the DATEPART() function −

SQL> SELECT DATEPART(MONTH, '2022-12-10') AS DATE_IN_MONTH;

Output

When we execute the above query, the DATEPART() function will return the value as 12 (integer value) −

+---------------+
| DATE_IN_MONTH |
+---------------+
| 12            |
+---------------+
sql-date-functions.htm
Advertisements