Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

True/False to Yes/No

Status
Not open for further replies.

quinnipiac0807

Programmer
Oct 25, 2004
38
US
Is there a way to change the data coming out of a database using an sql statement. For instance, I have a column that's bit. Instead of it saying true/false when it comes up on my page i want it to say yes/no. How do I do this with a sql statement?
 
Normally you would to that in the app, but it is possible to use a case statement in a SQL query to change the bit value column to a varchar(3) to hold the constants 'yes' or 'no'.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
You can use a CASE statement in your select (or perhaps create a view for ease of use and speed):

Code:
select id, title, bitfield, yesno=CASE bitfield when 1 then 'Yes' else 'No' end  
from table1

Hope this helps,
Kris

 
This is OK if bitfield cannot contain NULLs. Otherwise it will consider NULL as "No" which depending on app logic sometimes may be completely wrong.
 
Yes, quite true on the NULL issue. But could you simply add another WHEN clause (and turn some things around)? Such as:

Code:
select id, title, bitfield, 
yesno=CASE 
    when bitfield=1 then 'Yes' 
    when bitfield IS NULL then NULL 
    else 'No' end  
from table1

I haven't tried this, just shooting in the dark...

Cheers,
Kris

 
Yes that should work and you could even do this:
Code:
[Blue]SELECT[/Blue] id[Gray],[/Gray] title[Gray],[/Gray] bitfield[Gray],[/Gray] 
       yesno[Gray]=[/Gray][Blue]CASE[/Blue] bitfield 
                  [Blue]WHEN[/Blue] 1 [Blue]THEN[/Blue] [red]'Yes'[/red] 
                  [Blue]WHEN[/Blue] 0 [Blue]THEN[/Blue] [red]'No'[/red] [Blue]END[/Blue]  
   [Blue]FROM[/Blue] table1
The null should print on its own.
-Karl


[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top