简体   繁体   中英

How to return Yes or No in SQL without if/case flows

Apart from the IFs and the CASEs from SQL, is there another way to return a table of "Yes"/"No" (in SQL)?

EDIT: Yes, it's bad practice, and no I can't do so in the application (it's a school project with specific requirements)

The following is bad practice:

You can have a column that has the string values "Yes" and "No" and then just select from that column.

You will then have to maintain this columns values.

A better practice would be:

Have a bit column and then in the selects against that column have a case statement to return string values.

use an inline view to translate 0 to 'No' and and 1 to 'Yes' , though you could just stop at no.

SELECT yesNo.Option
FROM YourTable A
LEFT JOIN (SELECT 1 ID, 'YES' as Option UNION ALL SELECT 0, 'No') YesNo
 on A.Field1 = yesNo.ID

This assumes that 0 is in the results of mytable somewhere.

  1. Field1 should be the the column name having 0 or 1 or whatever in it you need to translate
  2. yourTable should be just that the table in question (or query)
  3. All this does is create an inline view with a list of hard coded translations. Better would be to have a translation table and simply use it.

A good idea would be the use of UNION between two selects, one with 'yes' and one with 'no'. But the above answers are enough. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM