How to Get Current Date and Time in SQL?
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’.
Select GetDate() AS 'CurrentDATETime';
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().
Select CURRENT_TIMESTAMP AS "CURRENTTIMESTAMP";
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:
SELECT SYSDATETIME() 'Current TIME using SYSDATETIME()'
Extract the Time Part Alone from the Current Date from SYSDATE
We can extract the time part from GETDATE() or CURRENT_TIMESTAMP().
SELECT CONVERT(VARCHAR(8), GETDATE(),108)'hh:mi:ss'
Please Login to comment...