Catch The Error

SQL>DECLARE
2 dayofweek VARCHAR2(200);
3 BEGIN
4 SELECT TO_CHAR(sysdate,'Day') INTO dayofweek FROM DUAL ;
5
6 IF dayofweek = 'Tuesday'
7 THEN
8 DBMS_OUTPUT.PUT_LINE('Aloha!!! Today Is Tuesday');
9 ELSE
10 DBMS_OUTPUT.PUT_LINE('Today is '||to_char(sysdate,'Day'));
11 END IF;
12 END;
13 /
Today is Tuesday

PL/SQL procedure successfully completed.
SQL>

What's wrong in the above anonymous block if anything at all.

Questions by saginandkishore   answers by saginandkishore

Showing Answers 1 - 31 of 31 Answers

ulricp

  • Jul 9th, 2008
 

There is nothing wrong with the code. To try it just cut and paste the code into a SQL script, substitute the current day for Tuesday and run it. The result is as expected.

The "error" is in the output. So I think the question is misleading although it does say "... error if any ...".

Regards
Ulric.

  Was this answer useful?  Yes

nagasankar

  • Aug 12th, 2008
 

You have to use the TRIM before TO_CHAR as given below

SELECT TRIM(TO_CHAR(sysdate,'Day')) INTO dayofweek FROM DUAL returns.

  Was this answer useful?  Yes

sriatit

  • Dec 1st, 2008
 

Actually there is no sytactical error in this code. It just that the if condition always evaluates to false 'coz the query returns the day of the week with trailing spaces(except for Wednesday) and thus will never be equal to 'Tuesday'. So, the output will be :

On Tuesday :

Today is Tuesday
On other days :
Today is Wednesday

The output 'Aloha!!! Today Is Tuesday' is never displayed.

javedans

  • Jul 8th, 2009
 

TO_CHAR(sysdate,'Day') is always return day name with some blank space, So its better to use TRIM function to exact match.
Like TRIM(TO_CHAR(sysdate,'Day'))
Or
You can match initial 3 character in the by using substr function.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions