Open In App

How to Get Current Date and Time in SQL?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

GETDATE() function is mostly used to find the current Date. It will return the DATETIME data type. This means it will Return the Current date with the current Time.

In this article, we will show to get the current date and time in SQL. In SQL whenever we need to insert and fetch the Current date and time. So For finding to Current Date and Time SQL have some Predefined function. We will Implement a few methods here. With the help of the below function. 

Basically, we can say that GETDATE() returns the current database system date and time in the format ‘YYYY-MM-DD hh:mm: ss. mmm’.

Syntax

SELECT GETDATE();

Query

Select GetDate() AS 'CurrentDATETime';
GETDATE

 

Output

GETDATE

 

How To Use CURRENT_TIMESTAMP() Function

It is also used to find the current TIMESTAMP means the current Date and Time. The CURRENT_TIMESTAMP function can be used the same that the GETDATE() function is used CURRENT_TIMESTAMP returns the same result as GETDATE(). 

Query

Select CURRENT_TIMESTAMP AS "CURRENTTIMESTAMP"; 
CURRENT_TIMESTAMP

 

Output

 

SYSDATETIME()

SYSDATETIME() function is also used to get the current TIME of the System on which the instance of SQL Server is running. SYSDATETIME() function provides more fractional seconds precision compared to the GETDATE() function. We can fetch the TIME part from the DATE and TIME value returned from the SYSDATETIME() function as below:

Query

SELECT SYSDATETIME() 'Current TIME using SYSDATETIME()'
SYSDATETIME

 

Output

SYSDATETIME

 

Extract the Time Part Alone from the Current Date from SYSDATE

 We can extract the time part from GETDATE() or CURRENT_TIMESTAMP().

Query

SELECT CONVERT(VARCHAR(8), GETDATE(),108)'hh:mi:ss'

SYSDATE

Output

SYSDATE

 


Last Updated : 30 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads