0
select 
    case 
       when ''!='abcd'
           then 'nodate'
       else 'date'
    end;

Here I hope '' stand for null, but when I execute this query I am getting 'date' as an ouput, rather than 'nodate'. What is the problem in this query? Why does ''!='abcd' condition checking become true?

2
  • 4
    you have problem in your problen Commented Mar 31, 2011 at 11:44
  • hi webarto.. what is problen? Commented Mar 31, 2011 at 11:45

4 Answers 4

2

After looking at comments for this answer, here is the explaination -

In your example, '' is considered as NULL in Oracle and hence '' != 'abcd' is evaluated to UNKNOWN, which is the reason the 'else clause' result is returned (and not the 'when clause' result).

If you want to check some column for NULL explicitly mention it.

...case when columnName is NULL then ... else ... end

or if you are checking for some column value to be 'abcd' then use -

...case when columnName != 'abcd' then ... else ... end
Sign up to request clarification or add additional context in comments.

8 Comments

sachin could u please look at the my query again?i updated now. as per your ans the condition is true, then it should return'nodate' right.why its returing 'date'?
Actually, in Oracle '' is the same as NULL. However, an equality test doesn't work for NULL: NULL = NULL would not be true. So you do need to use IS NULL to test if a value is NULL.
-1 because your first four sentences are false. Oracle doesn't know the difference between the empty string and NULL: they are both considered NULL.
@Sachin: NULL != 'abcd' should evalute to NULL in all decent rdbms systems, Oracle included...
@Sachin: Because NULL never produces TRUE in any comparison except IS NULL. NULL indicates an unknown value, so both NULL = x and NULL != x are considered unknown conditions, regardless of the value of x.
|
1

If you want check for NULL, try this:

value IS NULL

So, if you want to replace '' with NULL, don't put NULL != 'abcd' but:

'abcd' IS NOT NULL


if there is a variable there and not '', try:

IF (l_variable != 'abcd) or (l_variable IS NULL)

It would be better however, to post complete code and not a modified part of it.

Comments

0

use NULL to check if the value is NULL, also see http://www.dba-oracle.com/t_case_statement_else_null_value.htm

Comments

0

I think what you want is

case when YourColumn is null then 'nodate' else 'yes date' end

2 Comments

in my PL/SQL program i jus declared a variable as l_variable varchar(20); In later part of my program i used this variable to compare with a string... if l_variable != 'abcd' then statements; end if; according to the condition the program should go inside the if loop but thats not happening in my case.
Try: IF (l_variable != 'abcd) or (l_variable IS NULL)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.