We often need current Date/Time in our queries. We can get it using SQL Server’s built in GETDATE() function.
Here are some examples to demonstrate this concept:
Select current Date/Time:
SELECT GETDATE()
Select current Date/Time along with other columns:
GETDATE() function can also be used along with other columns in SELECT query, like following code snippet selects the current date along with OrderDate.
SELECT GETDATE(), OrderDate FROM Orders
Get current Date/Time in WHERE clause:
It can also be used in WHERE clause as well. Though following query is not completely accurate to get today’s Orders in terms of date only but it is just to give an idea that how this function can be used in WHERE clause:
SELECT * FROM Orders WHERE OrderDate = GETDATE()
SQL Server’s GETDATE() is very handy function which can be used wherever we need Current Date/Time.