sqloracle

Oracle update between dates


So I'm trying to update a table based off if a date is between a range of two dates, but rather than updating the field if the date falls outside the two ranges, its updating regardless

update tablex
set field = 1 
where tableid = 1
and to_date('10/14/2024', 'mm/dd/yyyy') not between trunc(sysdate) and trunc(sysdate - 15);

Shouldn't this skip updating the field as the 10/14/24 date is between sysdate and sysdate-15 (9/30/2024)?


Solution

  • It appears that your "to_date('10/14/2024', 'mm/dd/yyyy')" is not in the correct format.

    If the column that contains date is already in a date format, you'll want to do this:

    -- Replace your_date_column with the correct column name --

    update tablex
    set field = 1 
    where tableid = 1
    and (your_date_column not between trunc(sysdate - 15) and trunc(sysdate));
    

    If the column that contains date is NOT in a date format, you'll want to do this:

    -- Replace your_date_column with the correct column name --

    update tablex
    set field = 1 
    where tableid = 1
    and (to_date(your_date_column, 'MM/DD/YYYY') not between trunc(sysdate - 15) and trunc(sysdate));