SQL GETDATE Function

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 derived from the OS (Operating System) of the computer on which the server instance is running. The syntax of the GETDATE is

GETDATE()

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 has made?
  • How long an Employee associated with us? etc

SQL GETDATE Example 1

The SQL Server 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]
SQL GETDATE Function 1

In this example, we are going to show you some of the GETDATE 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];
SQL GETDATE Function 2

Sql Server GETDATE Function Example

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 DATEDIFF function to show the difference between the Employee Hire date and Today (GETDTE()).

SQL GETDATE Function 3