T-SQL Tutorial

GETDATE() Date Function


The SQL Server GETDATE() function is a built-in function that returns the current date and time of the system in which the SQL Server is running. The function returns the date and time value in the format of "yyyy-mm-dd hh:mm:ss.mmm". Here, yyyy stands for the year, mm stands for the month, dd stands for the day, hh stands for the hour, mm stands for the minute, ss stands for the second, and mmm stands for the millisecond.

The GETDATE() function is often used in SQL Server to record the current date and time when a particular event occurs, such as when a record is inserted or updated in a database table. For example, a trigger may be created to update a "last modified" field in a table whenever a record in that table is updated. The GETDATE() function can be used in the trigger to capture the current date and time when the update occurred.


Syntax

GETDATE()



Example

select getdate() as System_date;

System_date
2014-02-22 10:59:48.707

SELECT CONVERT (date, GETDATE()) as System_date,
CONVERT (time, GETDATE()) as System_time,
CONVERT (datetime, GETDATE()) as System_datetime,
CONVERT (datetime2, GETDATE()) as System_datetime2;

System_dateSystem_timeSystem_datetimeSystem_datetime2
2014-02-2211:04:10.43000002014-02-22 11:04:10.4302014-02-22 11:04:10.4300000

Conclusion

The GETDATE() is an date function and returns the current database system timestamp as a datetime. This value is the date of the operating system of the computer on which the instance of SQL Server is running.

It can be used in various parts of a SQL query, such as in the SELECT statement to retrieve the current date and time for a specific record, or in the WHERE clause to filter records based on a date range. The returned value is in the format 'YYYY-MM-DD HH:MI:SS' e.g. '2014-12-31 11:59:59'.