![]() |
There are many built-in functions to get the current date and time in SQL. In this article, we will discuss how to get the current date and time in SQL with examples. SQL Get Current Date and TimeThree in-built SQL functions to fetch current date and time are:
SQL GETDATE() FunctionSQL GETDATE() function returns the current database system date and time in the format ‘YYYY-MM-DD hh:mm: ss. mmm’. SyntaxThe GETDATE() function syntax is:
ExampleIn this example, we use the GetDate() function in SQL to get the current date and time. Query SELECT GetDate() AS 'CurrentDATETime'; Output ![]() Output Get Only Time Part from GETDATE() Function ExampleIn this example, we will get only the time part from the GETDATE() function in SQL. Query SELECT CONVERT(VARCHAR(8), GETDATE(),108)'hh:mi:ss' Output ![]() Time Part from GETDATE() Function SQL CURRENT_TIMESTAMP() FunctionSQL CURRENT_TIMESTAMP() function used to find the current TIMESTAMP (current Date and Time). The CURRENT_TIMESTAMP returns the same result as GETDATE(). SyntaxThe CURRENT_TIMESTAMP() function syntax is:
ExampleIn this example, we use the CURRENT_TIMESTAMP() function in SQL to get the current date and time. Query SELECT CURRENT_TIMESTAMP AS "CURRENTTIMESTAMP"; Output![]() Output SQL SYSDATETIME() FunctionSQL 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: SyntaxThe SYSDATETIME() function syntax is:
ExampleIn this example, we use the SYSDATETIME() function in SQL to get the current date and time. Query SELECT SYSDATETIME() 'Current TIME using SYSDATETIME()' Output ![]() Output |
Reffered: https://www.geeksforgeeks.org
SQL |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |