Skip to main content
Log inGet a demo
Back to SQL Dictionary
Advanced

SQL Relative Dates

What are SQL Relative Dates?

SQL Relative Dates refer to a technique for working with dates and times in a dynamic and flexible manner by expressing date calculations in terms of relative units, such as days, weeks, or months, rather than specifying fixed dates. Relative dates are particularly useful for querying and reporting when you need to perform date-based operations that adapt to changing data over time.

When you would use it

You would use SQL Relative Dates when you want to:

  1. Create dynamic date-based queries: Instead of hardcoding specific dates, use relative date expressions to perform calculations based on current or variable dates. For example, finding all orders placed in the last 7 days.

  2. Generate recurring reports: Automatically adjust date ranges in reports to ensure that they always display up-to-date data without manual adjustments.

  3. Calculate date intervals: Determine intervals between dates, such as the number of days between two events.

  4. Filter data based on time frames: Create queries that filter data based on various time windows, like months, quarters, or fiscal years.

  5. Implement date-driven logic: Use relative dates to drive conditional logic within your queries and applications.

Syntax

SQL Relative Dates are typically expressed using date functions and arithmetic. The exact syntax may vary depending on the database system you are using, but common elements include:

  • Date functions (e.g., CURRENT_DATE, NOW(), DATEADD, DATEDIFF, DATE_SUB, etc.).
  • Arithmetic operations for adding or subtracting relative units (e.g., +, -).
  • Interval units (e.g., DAY, MONTH, YEAR, etc.).

Parameter values

The specific values you use will depend on the type of relative date calculation you need:

  • Date functions: Use functions like CURRENT_DATE, NOW(), or GETDATE() to obtain the current date or timestamp.

  • Arithmetic operations: Use + to add days, months, or years, and - to subtract them.

  • Interval units: Specify the unit of time for your relative date calculations, such as DAY, MONTH, YEAR, etc.

Example query

Suppose you have an orders table "sales" with a "order_date" column, and you want to retrieve all orders placed in the last 7 days. You can use the following SQL query (syntax may vary depending on your database system):

SELECT *
FROM sales
WHERE order_date >= CURRENT_DATE - INTERVAL '7 DAYS';

Example table response

Assuming the "sales" table contains data like this:

| order_date  | customer_name |
| ----------- | ------------- |
| 2023-10-01  | John Doe      |
| 2023-10-05  | Jane Smith    |
| 2023-10-10  | Alice Johnson  |

The query mentioned earlier would return the following result:

| order_date  | customer_name |
| ----------- | ------------- |
| 2023-10-05  | Jane Smith    |
| 2023-10-10  | Alice Johnson  |

This result shows all orders placed in the last 7 days based on the relative date filter.

Use cases

  • Creating dynamic and adaptable queries.
  • Generating recurring reports with up-to-date data.
  • Calculating date intervals for analysis.
  • Filtering data based on specific time frames.
  • Implementing date-driven logic in applications and business processes.

SQL languages this is available for

SQL Relative Dates are a common feature in most modern relational database management systems (RDBMS). The specific syntax and functions may vary slightly between different database systems, but you can typically use relative date calculations in:

  • SQL Server
  • PostgreSQL
  • Oracle Database
  • MySQL
  • SQLite
  • IBM Db2
  • MariaDB

Be sure to consult the documentation for your specific database system for details on the supported date functions and relative date syntax.

Related

SQL Row Number Over Partition By

SQL LAST_INSERT_ID

SQL SESSION_USER

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes