The SQL GETDATE function is a Date and Time Function used to return the Current Date and Time. In this section, we use this Getdate function to find SQL Today’s date with a practical example.
The return value of this GETDATE in SQL Server derives from the OS (Operating System) of the computer on which the server instance is running.
The syntax of the SQL GETDATE function is
We can use this date function to find out:
- When was the data inserted or updated?
- Maintaining the Log files.
- When was the last order the customer made?
- How long have Employees associated with us? etc
SQL Server GETDATE Example
The GETDATE function returns datetime data type, and the format is: ‘yyyy-mm-dd hh:mm:ss.mmm’ (you can see, fractional seconds precision is 3).
SELECT GETDATE() AS [Current_Date]
In this example, we are going to show you some of the SQL GETDATE function examples.
In the second select statement, we used the DATEPART to display the year value from today’s date. Next, we used the DATENAME in SQL Server to display the Month name from today’s date. In the last, we used the DATEADD function to display the Tomorrow date.
-- SQL Today's date example SELECT 'Today' AS 'TODAY', GETDATE() AS [Current_Date]; SELECT 'Year' AS 'YEAR', DATEPART(year, GETDATE()) AS [Present_Year]; SELECT 'Month' AS 'MONTH', DATENAME(month, GETDATE()) AS [Month_Name]; SELECT 'Tomorrow' AS 'DAY', DATEADD(day, 1, GETDATE()) AS [Next_Day];
GETDATE Example 2
In this Date and Time Function example, we are going to check Employee details (such as: Which date we hired him, how long he associated with this company, etc.). For this SQL today’s date example, we are using the DATEDIFF function.
SELECT [FirstName] + ' '+ [LastName] AS [Full Name] ,[Occupation] ,[YearlyIncome] ,[HireDate] ,DATEDIFF (year, [HireDate], GETDATE()) AS [YEARS] ,DATEDIFF (quarter, [HireDate], GETDATE()) AS [QUARTERS] ,DATEDIFF (month, [HireDate], GETDATE()) AS [MONTHS] ,DATEDIFF (day, [HireDate], GETDATE()) AS [DAYS] FROM [Employee]
From the below screenshot, you can observe that we used the GETDATE along with the DATEDIFF function to show the difference between the Employee Hire and Today date.