SQL Functions   «Prev  Next»

Lesson 16 SQL GETDATE, SYSDATE, and TODAY
Objective Understand how to use the GETDATE, SYSDATE, and TODAY functions, and combine them with other date-related functions to write useful SQL queries.

SQL GETDATE, SYSDATE, and TODAY

Introduction to Date Functions

Most database management systems (DBMS) provide a function to retrieve the current date and time. The function name varies by system, so always check your DBMS documentation. Common function names include:

  1. GETDATE (SQL Server)
  2. SYSDATE (Oracle)
  3. TODAY (some systems, like MySQL)

These functions are often used in queries to compare a stored date with the current date or to perform calculations, such as finding the number of days, weeks, or years between dates.

Example: Calculating Date Differences

To calculate the time difference between a column's date and the current date, you can use functions like DATEDIFF. For example, if you have a table named MyTable with a column MyDate, the following SQL Server query calculates the number of weeks between MyDate and today:

SELECT DATEDIFF(WEEK, MyDate, GETDATE())
FROM MyTable;
        

Note: Replace GETDATE with the appropriate function for your DBMS (SYSDATE for Oracle, TODAY for MySQL, etc.).

Here’s an example using the PUBS database in SQL Server to calculate the number of days between the publication date (pubdate) and today:

        
SELECT title, DATEDIFF(DAY, pubdate, GETDATE()) AS DaysSincePublication
FROM titles;
        
    

SQL Server: Using GETDATE

In SQL Server, GETDATE returns the current date and time. You can use it to filter records based on date comparisons. For example, to find sales representatives hired before today in the SALESREPS table:

        
SELECT NAME, HIRE_DATE
FROM SALESREPS
WHERE HIRE_DATE < GETDATE();
        
    

This query correctly identifies employees hired in the past by comparing HIRE_DATE with the current date.


Oracle: Using SYSDATE

In Oracle, SYSDATE returns the system’s current date and time. To retrieve only the date:

        
SELECT SYSDATE AS "Current_Date"
FROM DUAL;
        
    

To display both date and time in a specific format, use the TO_CHAR function:

        
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS "Formatted_Date"
FROM DUAL;
        
    

This formats the output as, for example, "2025-07-24 09:22:00".

Oracle 9i and later also support CURRENT_DATE (current date without time) and CURRENT_TIMESTAMP (current date and time with timezone).


Combining with Other Date Functions

Date functions like GETDATE, SYSDATE, and TODAY are often combined with other built-in functions to extract or manipulate date components. Here are two common examples:

1. Extracting Date Parts

Functions like MONTH and YEAR extract specific parts of a date. For example, to list salespeople and their hire month in the SALESREPS table:

        
SELECT NAME, MONTH(HIRE_DATE) AS Hire_Month
FROM SALESREPS;
        
    

To find salespeople hired in a specific year (e.g., 2016):

        
SELECT NAME, MONTH(HIRE_DATE) AS Hire_Month
FROM SALESREPS
WHERE YEAR(HIRE_DATE) = 2016;
        
    

2. Formatting Dates

The TO_CHAR function (Oracle) or similar functions in other systems can format dates for display. For example, in Oracle:

        
SELECT NAME, TO_CHAR(HIRE_DATE, 'Day, Month DD, YYYY') AS Formatted_Hire_Date
FROM SALESREPS;
        
    

This outputs dates in a readable format, like "Thursday, July 24, 2025".


Standard SQL Date-Related Functions

The SQL standard defines several built-in functions for working with dates and times. Below is a curated list relevant to date manipulation:

Function Returns
CURRENT_DATE The current date
CURRENT_TIME (precision) The current time, with the specified precision
CURRENT_TIMESTAMP (precision) The current date and time, with the specified precision
EXTRACT (part FROM source) The specified part (DAY, HOUR, etc.) from a DATETIME value

These functions are supported by most SQL implementations, though syntax may vary slightly. Check your DBMS documentation for additional functions like DATEDIFF, MONTH, or YEAR.


Sql Date - Quiz

est your understanding of date functions with this brief quiz:
SQL Date - Quiz

SEMrush Software