DATE literal: In Oracle SQL, dates can be created using the DATE literal or TO_DATE function.
Note: The format is DATE 'YYYY-MM-DD' for the literal method.
- Use the DATE literal: DATE 'YYYY-MM-DD'
- Alternatively, use TO_DATE('string', 'format')
- Store the result in a variable or use directly
SELECT DATE '2024-03-15' creates March 15, 2024.
Run the query to see the date result.
The result shows March 15, 2024 in the appropriate format.
Use DATE '2024-03-15' to create March 15, 2024.
• Literal format: Use DATE 'YYYY-MM-DD' format
• Hyphen separator: Separate year, month, day with hyphens
• Zero-padding: Months and days should be two digits
• Practice Tip: Remember Y-M-D order for date literals
- SELECT DATE '2023-12-25' FROM dual; -- Christmas 2023
- SELECT DATE '2024-01-01' FROM dual; -- New Year 2024
- SELECT DATE '2024-10-31' FROM dual; -- Halloween 2024
- Always use YYYY-MM-DD format for date literals
- Pad single-digit months and days with zeros
- Use dual table for single-row queries
Q: What if I use an invalid date?
A: Oracle will return an error. For example, DATE '2024-02-30' fails because February doesn't have 30 days.
Q: Can I include time in DATE literal?
A: No, DATE literal only creates dates. Use TIMESTAMP for date and time.
Date comparison: Using comparison operators (<, >, =) to determine the relationship between two dates in Oracle SQL.
Note: Oracle compares dates based on their internal representation.
- Create two DATE literals or expressions
- Use comparison operator (<, >, =) between them
- Evaluate the boolean result
SELECT DATE '2024-03-15' < DATE '2024-03-20' FROM dual;
Oracle evaluates the comparison and returns TRUE/FALSE.
Since March 15 is before March 20, the result is TRUE.
Yes, March 15, 2024 is earlier than March 20, 2024.
• Comparison operators: Use <, >, = to compare dates
• Boolean result: Comparison returns 1 (true) or 0 (false)
• Chronological order: Earlier dates are less than later dates
• Practice Tip: Test with known date relationships first
- SELECT DATE '2024-01-01' < DATE '2024-12-31' FROM dual; -- returns 1 (true)
- SELECT DATE '2024-05-15' > DATE '2024-03-10' FROM dual; -- returns 1 (true)
- SELECT DATE '2024-04-01' = DATE '2024-04-01' FROM dual; -- returns 1 (true)
- < means "is earlier than", > means "is later than"
- Oracle stores dates as numbers internally for comparison
- Time components matter in date comparisons if present
Q: What comparison operators can I use?
A: You can use all comparison operators: <, >, <=, >=, =, !=, <>
Q: Does time matter in date comparison?
A: Yes! If dates include time, Oracle compares both date and time components.
Greater than (>): Operator that returns 1 if the left operand is later in time than the right operand.
Note: For dates, "greater than" means chronologically later.
- Create two DATE literals
- Use the > operator between them
- Evaluate the boolean result
SELECT DATE '2024-06-10' > DATE '2024-03-15' FROM dual;
Oracle evaluates the comparison and returns 1 for true.
Since June 10 is after March 15, the result is 1 (true).
Yes, June 10, 2024 is greater than March 15, 2024.
• Greater than: > operator returns 1 if left date is later
• Chronological logic: Later dates are greater than earlier dates
• Boolean evaluation: Result is 1 (true) or 0 (false)
• Practice Tip: Think of the number line for date ordering
- SELECT DATE '2024-12-31' > DATE '2024-01-01' FROM dual; -- returns 1
- SELECT DATE '2024-08-15' > DATE '2024-08-10' FROM dual; -- returns 1
- SELECT DATE '2024-02-01' > DATE '2024-12-31' FROM dual; -- returns 0
- > means "comes later in time"
- Think of dates on a timeline from left to right
- Any date in June > any date in March of the same year
Q: What if I compare dates in different years?
A: The year is considered first, then month, then day. SELECT DATE '2023-12-31' < DATE '2024-01-01' FROM dual; returns 1.
Q: Can I chain multiple comparisons?
A: Yes! You can use expressions like date1 > date2 AND date2 > date3.
Less than (<): Operator that returns 1 if the left operand is earlier in time than the right operand.
Note: For dates, "less than" means chronologically earlier.
- Create two DATE literals
- Use the < operator between them
- Evaluate the boolean result
SELECT DATE '2024-01-01' < DATE '2024-02-14' FROM dual;
Oracle evaluates the comparison and returns 1 for true.
Since January 1 is before February 14, the result is 1 (true).
Yes, January 1, 2024 is less than February 14, 2024.
• Less than: < operator returns 1 if left date is earlier
• Chronological logic: Earlier dates are less than later dates
• Boolean evaluation: Result is 1 (true) or 0 (false)
• Practice Tip: Remember: < means "earlier than"
- SELECT DATE '2024-01-01' < DATE '2024-12-31' FROM dual; -- returns 1
- SELECT DATE '2024-05-01' < DATE '2024-05-15' FROM dual; -- returns 1
- SELECT DATE '2024-10-31' < DATE '2024-09-15' FROM dual; -- returns 0
- < means "comes earlier in time"
- Think of the calendar year from January to December
- Any date in January < any date in February of the same year
Q: What happens if I compare the same dates?
A: SELECT DATE '2024-05-01' < DATE '2024-05-01' FROM dual; returns 0 since they're equal.
Q: Can I use <= for "less than or equal"?
A: Yes! SELECT DATE '2024-05-01' <= DATE '2024-05-01' FROM dual; returns 1.
Equality (=): Operator that returns 1 if both operands represent the same date and time.
Note: For dates, equality means identical year, month, and day.
- Create two DATE literals
- Use the = operator between them
- Evaluate the boolean result
SELECT DATE '2024-03-15' = DATE '2024-03-15' FROM dual;
Oracle evaluates the comparison and returns 1 for true.
Since both dates are identical, the result is 1 (true).
Yes, March 15, 2024 and March 15, 2024 are equal.
• Equality: = operator returns 1 if dates are identical
• Exact match: Year, month, and day must all match
• Boolean evaluation: Result is 1 (true) or 0 (false)
• Practice Tip: Use = to check for specific dates
- SELECT DATE '2024-12-25' = DATE '2024-12-25' FROM dual; -- returns 1
- SELECT DATE '2024-01-01' = DATE '2023-01-01' FROM dual; -- returns 0
- SELECT DATE '2024-07-04' = DATE '2024-07-04' FROM dual; -- returns 1
- = checks for exact date match
- Year, month, and day must all be the same
- Use for validating specific dates like birthdays
Q: What if dates have different times?
A: If you include time components, they must also match for equality.
Q: Can I check inequality with !=?
A: Yes! SELECT DATE '2024-03-15' != DATE '2024-03-16' FROM dual; returns 1.