SQL - SYSDATETIME() Function



The SQL SYSDATETIME() function is used to return the current date and time of the computer that the server instance is executing on. The SYSDATETIME() function is more accurate to fractional seconds than GETDATE().

Because the SQL SYSDATETIME() function is nondeterministic, views and columns with expressions referencing it cannot be indexed. The current date and time are returned as a datetime2(7) value by the SYSDATETIME() function.

SYSDATETIME() function is similar to GETDATE() function with little differences:

  • SYSDATETIME returns the fractional seconds up to 7, whereas the GETDATE returns the fractional seconds up to 3.

  • SYSDATETIME returns data type as datetime2, whereas GETDATE returns DateTime as data type.

Syntax

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

SYSDATETIME()

Parameters

This function does not accept any parameters.

Example

We can retrieve the date and time of the SQL server by using the following query −

SELECT SYSDATETIME() AS SysDateTime;

Output

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

+-----------------------------+
| SysDateTime                 |
+-----------------------------+
| 2023-02-17 11:58:23.0133201 |
+-----------------------------+

Example

In the following example, we are using the CONVERT() function with the sysdatetime() function for translating the output into the current date only. Execute the below query to do so −

SELECT CONVERT(DATE, SYSDATETIME()) AS CurrentDate;

Output

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

+-------------+
| CurrentDate |
+-------------+
| 2023-02-17  |
+-------------+

Example

Let us look into another example, where we are using the CONVERT() function with the sysdatetime() function to converting the output into the current time only by using the following query −

SELECT CONVERT(TIME, SYSDATETIME()) AS CurrentTime;

Output

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

+------------------+
| CurrentTime      |
+------------------+
| 12:11:37.1422573 |
+------------------+

Example

Here, we are going to use the DATEPART() to get only the part of the return value. Execute the below query to retrieve only month from the sysdatetime() −

SELECT DATEPART(month, SYSDATETIME()) AS Month;

Output

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

+-------+
| Month |
+-------+
| 2     |
+-------+

Example

The following query retrieves the particular year from sysdatetime() −

SELECT DATEPART(year, SYSDATETIME()) AS Year;

Output

On executing the above query, it will generate the following output as shown below −

+------+
| Year |
+------+
| 2023 |
+------+

Example

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

CREATE TABLE OTT(
   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 OTT table using INSERT statements as shown in the query below −

INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(1, 'Dhruv', 'Silver', '2022-12-05');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(2, 'Arjun','Platinum', '2021-03-01');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(3, 'Dev','Silver', '2021-03-15');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(4, 'Riya','Gold', '2022-12-05');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(5, 'Aarohi','Platinum', '2020-05-02');

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

SELECT * FROM OTT;

The table was created in the database and displayed as shown below −

+----+-----------------+------------+------------------+
| ID | SUBSCRIBER_NAME | MEMBERSHIP | SUBCRIPTION_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       |
+----+-----------------+------------+------------------+

Now, we are going to display the remaining number of days for the subscription plans to complete by running the following query:

SELECT SUBSCRIBER_NAME, SUBCRIPTION_DATE, DATEDIFF(DAY, SUBCRIPTION_DATE, SYSDATETIME()) AS REMAINING_DAYS FROM OTT;

Output

On executing the above query, it will generate the following output as shown below −

+-----------------+-------------------+----------------+
| SUBSCRIBER_NAME | SUBSCRIPTION_DATE | REMAINING_DAYS |   
+-----------------+-------------------+----------------+
| Dhruv           | 2022-12-05        | 74             |
| Arjun           | 2021-03-01        | 718            |
| Dev             | 2021-03-15        | 704            |
| Riya            | 2022-12-05        | 74             |
| Aarohi          | 2020-05-02        | 1021           |
+-----------------+-------------------+----------------+

Example

Let us create a another 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 EMPLOYEE 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 year using the column Date_Of_Birth using sysdatetime() by running the below query −

SELECT Name, Date_Of_Birth, Country, DATEDIFF(YEAR, Date_Of_Birth, SYSDATETIME()) AS Years FROM IPLPlayers;

Output

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

+------------+---------------+------------+-------+
| Name       | Date_Of_Birth | Country    | Years |
+------------+---------------+------------+-------+
| Shikhar    | 1999-11-10    | India      | 24    |
| Dhoni      | 1992-12-11    | Jharkhand  | 31    |
| Gayle      | 1993-10-12    | Westindies | 30    |
| Williamson | 1992-09-23    | NewZealand | 31    |
+------------+---------------+------------+-------+
sql-date-functions.htm
Advertisements