Many times we come across a scenario where we need to get the current Date & Time. There are multiple ways to get this information in Sql Server, here I am listing out few of them:
[ALSO READ] How to get Date Part only from DateTime in Sql Server
Approach 1: Using GETDATE() funtion
GETDATE() function returns the current Date and Time from the system on which the Sql Server is installed/running. Basically it derives the value from the operating system of the computer on which the Sql Server instance is running. The value returned from the GETDATE() function is of the type DATETIME.
Below example shows how we can use the GETDATE() function in the SELECT statement to get the current Date and Time.
SELECT GETDATE() 'Current Date and Time using GETDATE()'
[ALSO READ] How to get current TIME in Sql Server
Approach 2: Using CURRENT_TIMESTAMP function
This function is the ANSI SQL equivalent to GETDATE() function. But majority of the developers in Sql Server use the GETDATE() function instead of CURRENT_TIMESTAMP. Both functions return the same result. And return type of the CURRENT_TIMESTAMP function is also of the type DATETIME
Below example shows how we can use the CURRENT_TIMESTAMP function in the SELECT statement to get the current Date and Time.
SELECT CURRENT_TIMESTAMP AS 'Current Date and Time using CURRENT_TIMESTAMP'
From the above reult we can see that the Date and Time returned by the CURRENT_TIMESTAMP function is same as that of the GETDATE() function shown in the previous example.
[ALSO READ] Difference between DateTime and DateTime2 DataType
Approach 3: Using SYSDATETIME() funtion
SYSDATETIME() function can also be used to get the current Date and Time of the computer on which the instance of SQL Server is running. SYSDATETIME() function provides more fractional seconds precision compared to the GETDATE() function. Return type of the SYSDATETIME() function is of the type DATETIME2. This function is introduced in Sql Server 2008.
SELECT SYSDATETIME() 'Current Date and Time using SYSDATETIME()'