Rohit Kesarwani
Rohit Kesarwani

Reputation: 83

Issue with comparision of day in PL/SQL

I am working on PL/SQL. Meanwhile, I required to compare whether the date in table is friday or not. I applied the below code but it's still executing the 'else' part of program. Can anyone please tell me the way?

begin
    select dt into dat from ticket_booking where id=1;
    dy := to_char(dat, 'DAY');
    if  dy = to_char('FRIDAY') then
        dbms_output.put_line(dy);
    else
        dbms_output.put_line('didnt print'||dy);
    end if;
end;

Upvotes: 2

Views: 100

Answers (3)

Seyran
Seyran

Reputation: 721

As all above, I can add, if you don't want to use string compare use can do this comparison

to_char(dat, 'DAY', 'NLS_DATE_LANGUAGE=''numeric date language''')

it's like language but in numbers, where day of weeks numerated from 1 to 7, and you can compare result in numeric values.

Upvotes: 2

Avrajit Roy
Avrajit Roy

Reputation: 3303

Just replace 'DAY' with 'Fmday' and boom the code runs :).

DECLARE
  dat DATE;
  dy  VARCHAR2(100);
BEGIN
  SELECT dt INTO dat FROM ticket_booking WHERE id=1;
  dy   := TO_CHAR(dat, 'fmDAY');
  IF dy = TO_CHAR('FRIDAY') THEN
    dbms_output.put_line(dy);
  ELSE
    dbms_output.put_line('didnt print'||dy);
  END IF;
END;
/

Upvotes: 2

Nitish
Nitish

Reputation: 1736

Instead of DAY, you need to use fmDAY.

begin
select dt into dat from ticket_booking where id=1;
dy := to_char(dat, 'fmDAY');
if  dy = to_char('FRIDAY') then
    dbms_output.put_line(dy);
else
    dbms_output.put_line('didnt print'||dy);
end if;
end;

Upvotes: 2

Related Questions