SQL - GETDATE() Function
The SQL GETDATE() function is used to retrieve the current system date and time of the system where the SQL server instance is running.
This function does not require any parameters, instead, it returns current date and time as a datetime data type.
The format of the returned datetime value will be in âYYYY-MM-DD hh:mm:ss.mmmâ. Where,
YYYY represents the year.
MM represents the month.
DD represents the day.
hh represents the hour.
mm represents the minute.
ss represents the second.
mmm represents the millisecond.
Syntax
Following is the syntax of the SQL GETDATE() function −
GETDATE()
Parameters
This function does not accept any parameters.
Example
The following example demonstrates the usage of the SQL GETDATE() function −
SQL> SELECT GETDATE() AS DATE_AND_TIME;
Output
When we execute the above query, the output is obtained as follows −
+-------------------------+ | DATE_AND_TIME | +-------------------------+ | 2023-02-17 10:37:52.460 | +-------------------------+
Example
The result format of the GETDATE() function is yyyy-mm-dd hh:mm:ss.mmm. Now, let us customize the result format using the following query −
SQL> SELECT FORMAT(GETDATE(),'dd-mm-yyyy hh:mm:ss tt') AS FORMAT_DATE_AND_TIME
Output
If we execute the above query, the result is produced as follows −
+------------------------+ | FORMAT_DATE_AND_TIME | +------------------------+ | 17-40-2023 10:40:20 AM | +------------------------+
Example
We can use the CONVERT() function to return only the date or time part from the result of the GETDATE() function.
SQL> SELECT CONVERT(DATE, GETDATE()) AS CURRENTDATE, CONVERT(TIME, GETDATE()) AS CURRENTTIME;
Output
On executing the above query, the output is displayed as follows −
+-------------+------------------+ | CURRENTDATE | CURRENTTIME | +-------------+------------------+ | 2023-02-17 | 10:43:11.6300000 | +-------------+------------------+
Example
Here, we are trying to add and subtract days from the result of the GETDATE() function.
SQL> SELECT GETDATE() +10 AS DATE_ADD, GETDATE() -15 AS DATE_SUB;
Output
When we execute the above query, the output is obtained as follows −
+-------------------------+-------------------------+ | DATE_ADD | DATE_SUB | +-------------------------+-------------------------+ | 2023-02-27 11:57:43.340 | 2023-02-02 11:57:43.340 | +-------------------------+-------------------------+
Example
We can use the SQL GETDATE() function with the DAY, MONTH, and YEAR functions to display the current day, month, and year from the result of the GETDATE() function.
SQL> SELECT YEAR(GETDATE()) AS [YEAR], MONTH(GETDATE()) AS [MONTH], DAY(GETDATE()) AS [DAY];
Output
If we execute the above query, the result is produced as follows −
+------+-------+-----+ | YEAR | MONTH | DAY | +------+-------+-----+ | 2023 | 2 | 17 | +------+-------+-----+
Example
We have different functions in SQL (such as GETDATE(), CURRENT_TIMESTAMP, and SYSDATETIME() functions) to retrieve the current datetime, time, or date.
Now, let us try to execute the above three functions to understand the difference in their results.
SQL> SELECT GETDATE() AS [GETDATE_RESULT], CURRENT_TIMESTAMP AS [CT_RESULT], SYSDATETIME() AS [SYSTEMDATE_RESULT]
Output
As we can see in the output, all three functions are returning the same timestamp but the SYSDATETIME is giving more fractional seconds accuracy as compared to the other two functions.
+-------------------------+-------------------------+-----------------------------+ | GETDATE_RESULT | CT_DATE | SYSDATETIME_DATE | +-------------------------+-------------------------+-----------------------------+ | 2023-02-17 11:44:58.967 | 2023-02-17 11:44:58.967 | 2023-02-17 11:44:58.9679516 | +-------------------------+-------------------------+-----------------------------+
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> 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 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 − We can pass the GETDATE() function as an argument.
SQL> SELECT ID, NAME, DATE_OF_BIRTH, DATEDIFF(YEAR, DATE_OF_BIRTH, GETDATE()) 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 OTT in the SQL database using the CREATE statement as shown in the query below −
SQL> 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 −
SQL> 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'); INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(6, 'Lisa','Platinum', '2022-11-25'); INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(7, 'Roy','Gold', '2021-05-30');
We can verify whether the table OTT is created or not using the following query −
SQL> SELECT * FROM OTT
The table OTT 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 complete using the following query −
SQL> SELECT SUBSCRIBER_NAME, SUBCRIPTION_DATE, DATEDIFF(HOUR, SUBCRIPTION_DATE, GETDATE()) AS REMAINING_HOURS FROM OTT;
Output
If we execute the above query, the result is produced as follows −
+-----------------+-------------------+-----------------+ | SUBSCRIBER_NAME | SUBSCRIPTION_DATE | REMAINING_HOURS | +-----------------+-------------------+-----------------+ | Dhruv | 2022-12-05 | 1844 | | Arjun | 2021-03-01 | 17300 | | Dev | 2021-03-15 | 16964 | | Riya | 2022-12-05 | 1844 | | Aarohi | 2020-05-02 | 24572 | | Lisa | 2022-11-25 | 2084 | | Roy | 2021-05-30 | 15140 | +-----------------+-------------------+-----------------+