Data Manipulation   «Prev 

How to compare dates in Oracle

In Oracle, the best way to compare dates is to use the built-in DATE datatype and the comparison operators. The DATE datatype in Oracle stores both date and time information, making it ideal for storing and manipulating date and time values. Here are some ways to compare dates in Oracle:
  1. Comparison operators: The comparison operators (<, <=, >, >=, =, and !=) can be used to compare dates in Oracle. For example, to find all records where the date is greater than or equal to a certain date, you can use a query like this:
    SELECT * FROM mytable 
    WHERE mydate >= '01-JAN-2022';
    
  2. Date functions: Oracle provides several built-in date functions that can be used to manipulate and compare dates. For example, the TO_DATE function can be used to convert a string to a date datatype, and the TRUNC function can be used to truncate the time component of a date value. For example, to find all records where the date is greater than or equal to a certain date, truncated to the day, you can use a query like this:
    SELECT * FROM mytable 
    WHERE TRUNC(mydate) >= TO_DATE('01-JAN-2022', 'DD-MON-YYYY');
    
  3. Interval functions: Oracle provides several built-in interval functions that can be used to manipulate and compare date intervals. For example, the MONTHS_BETWEEN function can be used to find the number of months between two dates, and the ADD_MONTHS function can be used to add or subtract months from a date. For example, to find all records where the date is within a certain number of months from a given date, you can use a query like this:
    SELECT * FROM mytable 
    WHERE ABS(MONTHS_BETWEEN(mydate, '01-JAN-2022')) <= 6;
    
Question: How do I compare dates in Oracle?
Answer: In Oracle, dates have a DATE internal format and comparing DATES should be with matching data types, preferably a DATE to DATE datatype. If you avoid this data type mismatch and convert your text with the to_date function it is easy to compare dates in Oracle;
where my_date_col > to_date(2011-10-13,YYYY-MM-DD);

Below we compare to DATE in character format:

where to_char(my_char_col) > 2011-10-13

Comparing Dates the incorrect Way

These Oracle date comparisons works, but no index cannot be used because date2 is invalidated with the trunc function (unless you create a function-based index on trunc(date2,'YYYY:MM').

where to_char(DATE1,'YYYY:MM') >= to_char(DATE2,'YYYY:MM')
where trunc(date1,'mm') >= trunc(date2,'mm');

TO_DATE and TO_CHAR Formatting

TO_DATE and TO_CHAR are alike insofar as they both have powerful formatting capabilities. They are opposite insofar as TO_DATE converts a character string or a number into an Oracle date, whereas TO_CHAR converts an Oracle date into a character string. The formats for these two functions are as follows:
TO_CHAR(date[,'format'[,'NLSparameters']])
TO_DATE(string[,'format'[,'NLSparameters']])

date must be a column defined as a DATE datatype in Oracle. It cannot be a string, even if it is in the most common date format of DD-MON-YY. The only way to use a string where date appears in the TO_CHAR function is to enclose it within a TO_DATE function.
string is a 1) literal string, 2) literal number, or 3) database column containing a string or a number. In every case but one, the format of string must correspond to that described by format. Only if a string is in the default format can format be left out. The default starts out as "DD-MON-YY", but you can change this with

alter session set NLS_DATE_FORMAT = "DD/MON/YYYY";

for a given SQL session or with the NLS_DATE_FORMAT init.ora parameter.
format is a collection of many options that can be combined in virtually an infinite number of ways. Once you understand the basic method of using the options, putting them into practice is simple. NLSparameters is a string that sets the NLS_DATE_LANGUAGE option to a specific language, as opposed to using the language for the current SQL session. You should not need to use this option often. Oracle will return day and month names in the language set for the session with alter session.