Hey, readers! In our previous article, we had understood the working of SQL Server DATEPART() function. In this article, we will be focusing on SQL Server GETDATE() function.
Working of SQL Server GETDATE() function
SQL GETDATE()function is used to fetch the current date and time from the system.
The GETDATE() function renders the below format while representing the current timestamp i.e. the current date and time.
Having understood the working of SQL GETDATE() function, lets jump into understanding the syntax of the same.
Syntax of SQL Server GETDATE() function
Unlike other SQL Date functions, the GETDATE() function does not accept any parameter. It does return the current date and time in the below format —
Now, let us understand the implementation of this function through various examples in the below section.
Examples of SQL Server GETDATE() function
In the below example, we have used
SQL SELECT statement along with GETDATE() function to display the current date and time.
In this example, we have used
SQL Server DATEPART() function wherein we have passed the interval to be extracted as ‘month‘ from the input date as the current date obtained through
GETDATE() function as a parameter to the DATEDIFF() function.
SELECT DATEPART(month, GETDATE()) AS Output;
GETDATE() v/s CURRENT_TIMESTAMP()
While understanding the SQL Server Date Functions, we may come across the below question.
What’s the difference between GETDATE() function and CURRENT_TIMESTAMP() function, if both the functions return exactly the same value!?
Well, both functions have the same implementation. The only basic difference spotted is the origin of the functions i.e. CURRENT_TIMESTAMP() is an ANSI SQL function while GETDATE() is a T-SQL function.
By this, we have come to the end of this topic. Please feel free to comment below if you come across any doubts.
For more such posts on similar topics related to SQL Server, please do visit SQL Server JournalDev.