I Want convert bit
type to Yes or No
For Example:
SELECT FirstName, LastName, IsMale from members
Results:
Ramy Said 1
Expected Result:
Ramy Said Yes
I Want convert bit
type to Yes or No
For Example:
SELECT FirstName, LastName, IsMale from members
Results:
Ramy Said 1
Expected Result:
Ramy Said Yes
Here you go:
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.) TheELSE
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 largerCASE
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.You can do this using a searched case expression:
Use an
IIF()
which is availableSQL Server 2012
onwards.There is one another way of achieving this. (shorthand way(maybe not)). That's using
IIF()
. See below.SQL Server 2012 introduces two new keywords
FORMAT
andIIF
that can provide a more compact means of converting an integer or bit to string:But to convert a
bit
type toYes
orNo
I would now use:Use a
CASE
;