5

I am using following for select statement with case. The column to which the CASE is applied has True, False and Null values. I am trying to display True for True and False for both False and Null. But it give me an error message saying:

"Conversion failed when converting the varchar value 'Null' to data type bit."

This is my query:

    case
       when prog.has_post_calc_screen = ''True'' then ''True''
       when prog.has_post_calc_screen = ''False''then ''False''
       when prog.has_post_calc_screen = ''Null'' then ''False''
    End as Referal_ID,

Input which I am giving is:

 '1','ALL', 'ALL'

3 Answers 3

3

Instead of when prog.has_post_calc_screen = ''Null'' write when prog.has_post_calc_screen IS NULL THEN ... (You might also write ELSE ... because you have already processed true and false, so the only possible value is NULL).

UPDATE You can also write just

 case
   when prog.has_post_calc_screen = true then 'True'
   else 'False'
 END ....
Sign up to request clarification or add additional context in comments.

2 Comments

I can run the query with the error but the gridview(where the data is displayed) has empty cell when the value is Null but can see both True and False in records!!
Thank you a1ex07. The second case statement worked. Really appreciate it.
1

You cannot test for a NULL value with an =. You must use IS NULL instead, so change the one line of your CASE:

when prog.has_post_calc_screen IS NULL then ''False''

1 Comment

I can run the query with the error but the gridview(where the data is displayed) has empty cell when the value is Null and can see both True and False in records in the gridview!! any idea why?
1
when COALESCE(prog.has_post_calc_screen,''False'') = ''False''then ''False''

1 Comment

+1 beat me to it - just use COALESCE since the only case you want to change is a 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.