Getdate in SQL Server What is that? It is the command that will help you get current date in SQL Server and much more. Here's everything you need to know about getdate function in SQL Server.
GETDATE() function in SQL Server is an inbuilt function that returns the date and time of the system on which SQL Server is running. This function returns the date & month value in the format “yyyy-mm-dd hh:mm:ss.mmm”. In there:
- yyyy – year
- mm – month
- dd – day
- hh – now
- mm – minute
- ss – seconds
- mmm – milliseconds
getdate() function commonly used in SQL Server to get the current date when an event occurs, such as when a log record is inserted or updated in a database table. For example, a trigger can be created to update the last edited field in the table, whenever there is updated content.
Outstanding features of the getdate function in SQL Server:
- This function is used to find the current date and time of the database system.
- This function is in the date functions (Date).
- This function does not accept any parameters.
- This function returns results in the format 'YYYY-MM-DD hh:mm:ss.mmm'.
Syntax
To use the GETDATE function in SQL Server, we use the following syntax:
GETDATE()
There are no parameters or arguments in the statement.
Note:
- The GETDATE function returns the system date and time in the format 'yyyy-mm-dd hh:mi:ss.mmm'.
- See also CURRENT_TIMESTAMP function.
- GETDATE can be used in the following versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005.
For example
Take a look and explore some examples of the GETDATE function in SQL Server.
SELECT GETDATE();
Result: '2019-02-25 18:11:00.160'
Compare the function to get the current date in SQL Server with other date-time functions
SQL Server provides many date and time functions to return the current date and time, a date, or just the hour based on your requirements. If you want to return a date-time result in some format, you can use any of these suitable functions.
Consider the following example, where you have fetched the current date and time using various functions. You can compare them with other functions to understand exactly what results a particular function returns.
SELECT GETDATE() AS [Getdate],
CURRENT_TIMESTAMP AS [Current Timestamp],
SYSDATETIME() AS [Sysdatetime]
GO
The results appear in the image below. All these 3 functions are returning the same timestamp but SYSDATETIME is giving fractional second precision compared to the rest of the two functions.
As you can see the timestamp returned by the above functions has a certain format. If you want to get only the current system date or just the system time, you need to use another SQL Server function, specifically CONVERT to return only the date or time.
Previous article: DAY function in SQL Server
Next article: GETUTCDATE function in SQL Server