Convert bit type to Yes or No by query Sql Server 2005

后端 未结 5 923
一整个雨季
一整个雨季 2021-01-07 17:48

I Want convert bit type to Yes or No

For Example:

SELECT FirstName, LastName, IsMale from members
         


        
5条回答
  •  不思量自难忘°
    2021-01-07 18:05

    Here you go:

    SELECT FirstName, LastName, CASE WHEN IsMale = 1 THEN 'Yes' ELSE 'No' END AS Male
    FROM members
    

    Basically, you use a CASE statement to allow you to convert the value. If you had three choices, you could still use the case statement and just add another option (obviously you can't have three options with a bit but if the field was an int, etc.) The ELSE statement is the default statement that runs if you don't get a match. In our case, we just use it for No since we can only have yes or no but in the case of a larger CASE statement, you would want to use this as your fall-back field. For example, you could say "Item Not Found" if you were converting items.

提交回复
热议问题