SQL GETDATE Function Example

SQL GETDATE function is used to return the current system TIMESTAMP as a DATETIME value without the database time zone offset. The value is derived from the Operating System of the server on which the instance of SQL Server is running at present.

SQL GETDATE

The GETDATE() function returns the current database system date and time, in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format.

SQL GETDATE() function returns the current system timestamp as the DATETIME value without the database time zone offset.

The DATETIME value is derived from the OS of the server on which the instance of SQL Server is running.

Syntax

SELECT GETDATE();

Note

  1. The function returns the system date and time in the format ‘yyyy-mm-dd hh:mi:ss.mmm’.
  2. It is a nondeterministic function; therefore, we cannot create an index for columns that reference this function in the Views.

SQL Server Version

SQL GETDATE() function can be used in the following versions of SQL Server (Transact-SQL):

SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005.

Example of GETDATE() Function

How to get current system date and time in SQL.

Query

SELECT GETDATE();

Output

2019-11-13 15:13:26.270

How to get the Current System Date in SQL example.

Query

SELECT CONVERT (DATE, GETDATE ()) AS CURRENT_DATE;

Output

2019-11-13

Here, we have used a CONVERT() function to convert the DATETIME value to a DATE.

We can also use the TRY_CONVERT() and CAST() functions to convert the result of the GETDATE() function to date replace the CONVERT function with TRY_CONVERT() and CAST().

Query

SELECT TRY_CONVERT (DATE, GETDATE ()), CAST (GETDATE () AS DATE);

Output

2019-11-13

How to get the Current System Time in SQL example.

Query

SELECT CONVERT (TIME, GETDATE ()), TRY_CONVERT (TIME, GETDATE ()), CAST (GETDATE () AS TIME);

Output

16:12:35.8666667

Here, we have used the CONVERT (), TRY_CONVERT (), or CAST () function to convert the result of the GETDATE () function to time.

SQL Server GETDATE() function to get the current system date

If we want to get the current date, you can use the CONVERT() function to convert the DATETIME value to a DATE as follows.

Query

SELECT 
    CONVERT(DATE, GETDATE()) [Current Date];

Output

2019-11-26

Similarly, you can use the TRY_CONVERT() and CAST() functions to convert the result of the GETDATE() function to a date:

Query

SELECT 
    TRY_CONVERT(DATE, GETDATE()),
    CAST(GETDATE() AS DATE);

SQL Server GETDATE() function to get the current system time

If we need to get the current time only, you can use the CONVERT(), TRY_CONVERT(), or CAST() functions to convert the result of the GETDATE() function to a time:

Query

SELECT 
    CONVERT(TIME,GETDATE()),
    TRY_CONVERT(TIME, GETDATE()),
    CAST(GETDATE() AS TIME);

Conclusion

If you want to get the current date and timestamp without any local zone or settings, then you can use the SQL GETDATE() function.

Finally, SQL GETDATE Function Example is over.

Related Posts

SQL SYSDATETIME Function

SQL Format Function

SQL Date Functions

SQL Translate Function

SQL Datatypes Example

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.