GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  Basics
Go To First  |  Previous Question  |  Next Question 
 Basics  |  Question 45 of 49    Print  
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.




  
Total Answers and Comments: 9 Last Update: July 08, 2009     Asked by: saginandkishore 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sriatit
 
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.



Above answer was rated as good by the following members:
usha_martin
June 28, 2008 07:42:41   #1  
Sainathreddy Member Since: June 2008   Contribution: 1    

RE: Catch The Error
set server output on is missing
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
July 01, 2008 15:27:36   #2  
sandy1970 Member Since: July 2008   Contribution: 1    

RE: Catch The Error
SELECT TO_CHAR(sysdate 'Day') INTO dayofweek FROM DUAL returns

"Name of day padded with blanks to length of 9 characters"


IF 'Tuesday ' 'Tuesday' is not ture


 
Is this answer useful? Yes | No
July 09, 2008 16:41:13   #3  
ulricp Member Since: March 2008   Contribution: 1    

RE: Catch The Error
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.

 
Is this answer useful? Yes | No
August 12, 2008 09:59:54   #4  
nagasankar Member Since: August 2008   Contribution: 2    

RE: Catch The Error
You have to use the TRIM before TO_CHAR as given below

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

 
Is this answer useful? Yes | No
September 24, 2008 01:40:48   #5  
Praveen2384 Member Since: September 2008   Contribution: 7    

RE: Catch The Error
No out put is because we have to set setserverout
 
Is this answer useful? Yes | No
October 10, 2008 09:43:52   #6  
nagellikiran1283 Member Since: October 2008   Contribution: 1    

RE: Catch The Error
There is nothing wrong in it.
 
Is this answer useful? Yes | No
October 30, 2008 12:40:57   #7  
lamoscow Member Since: October 2008   Contribution: 1    

RE: Catch The Error
The output should be: Aloha!!! Today is Tuesday
 
Is this answer useful? Yes | No
December 01, 2008 15:10:36   #8  
sriatit Member Since: December 2008   Contribution: 2    

RE: Catch The Error
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.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 08, 2009 14:02:26   #9  
javedans Member Since: January 2009   Contribution: 8    

RE: Catch The Error
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.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape