-1

I have a date coming in the format 2022-05-26T13:50:44.000Z, 2018-10-05T18:14:00.000Z. I want the date to be picked after 2022 only from this column. But when I am using the below query it is not working at all.

 to_date(TO_CHAR(TRUNC(Paaf.last_update_date),'MM-DD-YYYY'),'MM-DD-YYYY') >= TO_DATE('01-01-2022','MM-DD-YYYY')

How to compare the date so that it only the dates after 01-01-2022 are picked

If i do Paaf.last_update_date like '%2022%' I am getting the output, is there anyway I can do '%Current year%' for the like to work

SSA_Tech124
  • 577
  • 1
  • 9
  • 25

3 Answers3

0

You don't need to truncate then convert to a string and convert back to a date.

Just compare the DATE column to a date literal:

SELECT *
FROM   table_name paaf
WHERE  paaf.last_update_date >= DATE '2022-01-01'

If you want dates after 2022-01-01 00:00:00 then use > rather than >=.

MT0
  • 143,790
  • 11
  • 59
  • 117
0

Assuming the date part format to be with timezone. Using cast and to_utc_timestamp_tz oracle functions in following manner.

with x as ( select CAST(to_utc_timestamp_tz('2022-05-26T13:50:44.000Z') AS DATE) as sample_year from dual ) select * from x where sample_year > TO_DATE('2021-01-01','YYYY-MM-DD');

gameiplay0
  • 71
  • 4
0

Just did some check as below, it should be able to help.

create table paaf(last_update_date timestamp);
insert into paaf values(TIMESTAMP '2021-12-01 05:30:00');

1 rows affected

insert into paaf values(TIMESTAMP '2022-12-01 05:30:00');

1 rows affected

insert into paaf values(DATE '2022-01-01');

1 rows affected

select * from paaf where last_update_date >= TIMESTAMP '2022-01-01 00:00:00';
| LAST_UPDATE_DATE          |
| :------------------------ |
| 01-DEC-22 05.30.00.000000 |
| 01-JAN-22 00.00.00.000000 |

db<>fiddle here

SeanH
  • 538
  • 2
  • 8