Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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   
like image 883
Ramy Said Avatar asked May 15 '11 13:05

Ramy Said


People also ask

How do I return a bit value in SQL Server?

We need to directly return the calculated BIT value based on condition. So we use the CAST() method available in SQL. We can use the CAST method to cast from one data type to another. You might want to cast a float value to int value for that we use CAST().

How do I convert bit to int in SQL?

Try using CAST(columnName AS INT) AS IntValue . e.g. OR you can use CONVERT(INT, columnName) AS IntValue .

How do I select a bit value in SQL?

You can use the CONVERT operator. CAST or CONVERT will work. Show activity on this post. 1 is the display for a true bit.

How do I cast a bit in SQL?

Data Type Conversions using the SQL CAST Function. For example, if we want to cast a Boolean value from the bit data type to a tiny int data type, we can do it with a valid expression such as: DECLARE @mybit BIT = 1; SELECT Test = CAST(@mybit AS TINYINT); Not every data type can be converted to all possible data types.


2 Answers

You can do this using a searched case expression:

SELECT     FirstName,     LastName,     CASE WHEN IsMale = 1 THEN 'Yes' ELSE 'No' END AS IsMale FROM     Members 
like image 185
Chris Fulstow Avatar answered Nov 01 '22 00:11

Chris Fulstow


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.

like image 31
IAmTimCorey Avatar answered Nov 01 '22 00:11

IAmTimCorey