SQL - CURRENT_TIMESTAMP Function



The SQL CURRENT_TIMESTAMP function is a built-in function that retrieves the current database system timestamp as a datetime value, without the database time zone offset. This function derives the datetime value from the operating system of the computer on which the instance of SQL Server runs. The default format of the timestamp will be yyyy:mm:dd hh:mm:ss.mmm.

The default format of the timestamp will be yyyy:mm:dd hh:mm:ss.mmm. We can also modify the format of the timestamp using the SQL format() function.

This function is similar to the SQL GETDATE() function. It is recommended to use the CURRENT_TIMESTAMP function instead of GETDATE() function.

Syntax

Following is the syntax of the SQL CURRENT_TIMESTAMP function −

CURRENT_TIMESTAMP

Parameters

This function does not accept any parameters.

Example

In the following example, we are trying to retrieve the date and time at the moment using the following query −

SQL> SELECT CURRENT_TIMESTAMP AS CURRENT_DATE_AND_TIME;

Output

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

+--------------------------+
| CURRENT_DATE_AND_TIME    |
+--------------------------+
| 2023-02-15 16:57:38.100  |
+--------------------------+

Example

The following query adds 10 days to the current date and time −

SQL> SELECT CURRENT_TIMESTAMP + 10 AS CURRENT_DATE_AND_TIME;

Output

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

+--------------------------+
| CURRENT_DATE_AND_TIME    |
+--------------------------+
| 2023-02-25 16:57:38.100  |
+--------------------------+

Example

The following query subtracts 14 days from the current date and time −

SQL> SELECT CURRENT_TIMESTAMP - 14 AS CURRENT_DATE_AND_TIME;

Output

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

+--------------------------+
| CURRENT_DATE_AND_TIME    |
+--------------------------+
| 2023-02-01 16:57:38.100  |
+--------------------------+

Example

In the example below, we are going to change the format of the CURRENT_TIMESTAMP to dd-MM-yyyy hh:mm:ss

SQL> SELECT FORMAT(CURRENT_TIMESTAMP, 'dd-MM-yyyy hh:mm:ss') AS CURRENT_DATE_AND_TIME;

Output

If we execute the above query, the result is produced as follows −

+-------------------------+
| CURRENT_DATE_AND_TIME   |
+-------------------------+
| 15-02-2023 05:11:41     |
+-------------------------+

Example

We can also use CURRENT_TIMESTAMP as an argument to the DATEDIFF() function. Let us create a table with the name EMPLOYEE in the SQL database using the CREATE statement as shown in the query below −

SQL> CREATE TABLE EMPLOYEE(ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, DATE_OF_BIRTH VARCHAR (20));

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', '2000-12-05');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(2, 'Arjun', '2000-03-01');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(3, 'Dev', '2001-03-15');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(4, 'Riya', '2003-12-05');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(5, 'Aarohi', '2000-05-02');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(6, 'Lisa', '1999-11-25');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(7, 'Roy', '2001-05-30');

We can verify whether the table EMPLOYEE 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  | 2000-12-05     |
| 2   | Arjun  | 2000-03-01     |
| 3   | Dev    | 2001-03-15     |
| 4   | Riya   | 2003-12-05     |
| 5   | Aarohi | 2000-05-02     |
| 6   | Lisa   | 1999-11-25     |
| 7   | Roy    | 2001-05-30     |
+-----+--------+----------------+

We can calculate the age of every employee in years using the following query −

Note − The CURRENT_TIMESTAMP retrieves the date and time at the moment.

SQL> SELECT ID, NAME, DATE_OF_BIRTH, DATEDIFF(YEAR, DATE_OF_BIRTH, CURRENT_TIMESTAMP) AS AGE_IN_YEARS FROM EMPLOYEE;

Output

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

+-----+--------+----------------+--------------+
| ID  | NAME   | DATE_OF_BIRTH  | AGE_IN_YEARS |   
+-----+--------+----------------+--------------+
| 1   | Dhruv  | 2000-12-05     | 23           |
| 2   | Arjun  | 2000-03-01     | 23           |
| 3   | Dev    | 2001-03-15     | 22           |
| 4   | Riya   | 2003-12-05     | 20           |
| 5   | Aarohi | 2000-05-02     | 23           |
| 6   | Lisa   | 1999-11-25     | 24           |
| 7   | Roy    | 2001-05-30     | 22           |
+-----+--------+----------------+--------------+

Example

Let us create another table with the name NETFLIX in the SQL database using the CREATE statement as shown in the query below −

SQL> CREATE TABLE NETFLIX(ID INT NOT NULL, SUBSCRIBER_NAME VARCHAR (200) NOT NULL, MEMBERSHIP VARCHAR (200), SUBCRIPTION_DATE DATE NOT NULL);

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

SQL> INSERT INTO NETFLIX(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(1, 'Dhruv', 'Silver', '2022-12-05');
INSERT INTO NETFLIX(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(2, 'Arjun','Platinum', '2021-03-01');
INSERT INTO NETFLIX(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(3, 'Dev','Silver', '2021-03-15');
INSERT INTO NETFLIX(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(4, 'Riya','Gold', '2022-12-05');
INSERT INTO NETFLIX(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(5, 'Aarohi','Platinum', '2020-05-02');
INSERT INTO NETFLIX(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(6, 'Lisa','Platinum', '2022-11-25');
INSERT INTO NETFLIX(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(7, 'Roy','Gold', '2021-05-30');

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

SQL> SELECT * FROM NETFLIX

The table NETFLIX is successfully created in the SQL database.

+-----+-----------------+------------+------------------+
| ID  | SUBSCRIBER_NAME | MEMBERSHIP | SUBSCRIPTION_DATE|   
+-----+-----------------+------------+------------------+
| 1   | Dhruv           | Silver     | 2022-12-05       |
| 2   | Arjun           | Platinum   | 2021-03-01       |
| 3   | Dev             | Silver     | 2021-03-15       |
| 4   | Riya            | Gold       | 2022-12-05       |
| 5   | Aarohi          | Platinum   | 2020-05-02       |
| 6   | Lisa            | Platinum   | 2022-11-25       |
| 7   | Roy             | Gold       | 2021-05-30       |
+-----+-----------------+------------+------------------+

We can display the remaining number of days for the subscription plans to be complete using the following query −

SQL> SELECT SUBSCRIBER_NAME, SUBCRIPTION_DATE, DATEDIFF(DAY, SUBCRIPTION_DATE, CURRENT_TIMESTAMP) AS REMAINING_DAYS FROM NETFLIX;

Output

If we execute the above query, the result is produced as follows −

+-----------------+-------------------+-----------------+
| SUBSCRIBER_NAME | SUBSCRIPTION_DATE | REMAINING_DAYS  |   
+-----------------+-------------------+-----------------+
| Dhruv           | 2022-12-05        | 72              |
| Arjun           | 2021-03-01        | 716             |
| Dev             | 2021-03-15        | 702             |
| Riya            | 2022-12-05        | 72              |
| Aarohi          | 2020-05-02        | 1019            |
| Lisa            | 2022-11-25        | 82              |
| Roy             | 2021-05-30        | 626             |
+-----------------+-------------------+-----------------+
sql-date-functions.htm
Advertisements