Mastering Date/Time Arithmetic: An Operations Guide for Oracle SQL

Data Types_en

The ability to handle dates and times in Oracle SQL is essential for data extraction, aggregation, and improving operational efficiency. This article provides a careful explanation for beginners, covering everything from the basics of date/time arithmetic to practical applications. We’ll include explanations and examples to support a deeper understanding.

1. Basics of Date/Time Data in Oracle

When handling dates and times in Oracle SQL, the following data types are used.

Data TypeCharacteristicsUse Case
DATEHolds year, month, day, hour, minute, and second.SYSDATE (getting the current date/time), etc.
TIMESTAMPAdds fractional seconds to the DATE type.High-precision time recording.
TIMESTAMP WITH TIME ZONEDate/time with a time zone.Time calculations for global systems.
INTERVAL YEAR TO MONTH / DAY TO SECONDA data type for expressing a duration (difference).Calculating project duration.

2. Basic Date/Time Arithmetic Operations

Oracle makes it easy to perform arithmetic using numeric and date-type data.

Adding or Subtracting Days from a Date

Example 1: Find the date 7 days from the current date/time

SELECT SYSDATE + 7 AS one_week_later
FROM DUAL;

Example Output

SQL> SELECT SYSDATE + 7 AS one_week_later
2 FROM DUAL;

ONE_WEEK_
---------
22-NOV-25

Example 2: Find the date 5 days before a specific date

SELECT TO_DATE('2025-12-31', 'YYYY-MM-DD') - 5 AS five_days_earlier
FROM DUAL;

Example Output

SQL> SELECT TO_DATE('2025-12-31', 'YYYY-MM-DD') - 5 AS five_days_earlier
2 FROM DUAL;

FIVE_DAYS
---------
26-DEC-25

Calculating the Difference Between Dates

Example 3: Calculate the number of days until the end of the year

SELECT TO_DATE('2025-12-31', 'YYYY-MM-DD') - SYSDATE AS days_until_year_end
FROM DUAL;

Point: The result is a number. The fractional part indicates the time.

Example Output

SQL> SELECT TO_DATE('2025-12-31', 'YYYY-MM-DD') - SYSDATE AS days_until_year_end
2 FROM DUAL;

DAYS_UNTIL_YEAR_END
-------------------
45.8741551

3. Time Unit Calculations and Specific Examples

In Oracle, you can perform calculations in units of time by adding or subtracting numbers from date/time types.

UnitExampleFormula
1 Day+1 Day+ 1
1 Hour+5 Hours+ (5/24)
1 Minute+30 Minutes+ (30/1440)
1 Second+10 Seconds+ (10/86400)

Example 4: Calculate 5 hours from the current date/time

SELECT SYSDATE + (5/24) AS five_hours_later
FROM DUAL;

Example 5: Calculate 15 minutes later

SELECT SYSDATE + (15/1440) AS fifteen_minutes_later
FROM DUAL;

4. Applied Examples by Practical Scenario

Scenario 1: Get order history from the past 30 days

SELECT ORDER_ID, ORDER_DATE
FROM ORDERS
WHERE ORDER_DATE >= SYSDATE - 30;

Scenario 2: Calculate days worked

An example of calculating the number of days worked for each employee.

SELECT EMPLOYEE_ID, 
       TRUNC(SYSDATE - HIRE_DATE) AS days_worked
FROM EMPLOYEES;

Example Output

EMPLOYEE_ID DAYS_WORKED
----------- -----------
        101         365
        102         720

Scenario 3: Extract transactions within business hours

SELECT CUSTOMER_ID, TRANSACTION_TIME
FROM TRANSACTIONS
WHERE TO_CHAR(TRANSACTION_TIME, 'HH24:MI') BETWEEN '09:00' AND '18:00';

Scenario 4: Calculate the next scheduled maintenance date

SELECT ADD_MONTHS(SYSDATE, 1) AS next_maintenance_date
FROM DUAL;

5. Common Errors and Solutions

Error Example 1: Date format mismatch

SELECT TO_DATE('2024-31-12', 'YYYY-MM-DD') FROM DUAL;

Solution: Specify the correct format.

SELECT TO_DATE('2024-12-31', 'YYYY-MM-DD') FROM DUAL;

Error Example 2: Calculation error due to NULL value

If a NULL value is included, the operation result will also be NULL.

Avoidance: Set a default value using the NVL function.

SELECT NVL(SYSDATE + NULL, SYSDATE) AS default_date
FROM DUAL;

Summary

Mastering date/time arithmetic can significantly improve your operational efficiency. Please use the basic and applied examples introduced in this article to streamline your daily data operations.

Next, try tackling more advanced date/time operations (like INTERVAL types and time zone operations). If you have any questions, we look forward to your comments and feedback!

[reference]
Oracle Database SQL Language Reference, 19c

コメント

Copied title and URL