Display Yes/No from bit by default - sql

I have a column that is of type 'bit'. I want the column to display either the word yes (1) or (0) if no. Is there somewhere I can set this?
If not is there a replacement for boolean I can use? True/false will do fine as a replacement
I am using SQL Server-management 2008 R2
I'm trying to amend the table.

Try using case:
select case
when MyBitField = 1 then
'yes'
else
'no'
end
from MyTable

Related

Tsql: LOWERCASE using with IN

I have a question concerning the lower function.
,case when lower(replace(producttitle, ‘ ‘, ‘‘)) in (‘microphone‘) then ‘YES‘ else ‘NO‘ end
The statement above does select only YES, when column value = value in the IN function.
I want to get a YES when the value such as it is written in the IN function. When the column value is ‘Microphone‘, i want to get it lowercase —> microphone and because the IN containts ‘microphone‘, I should get a YES, right?
When the column value is Microphone (upper B), why do I not get it lower? Such as in the IN function.
Example: column value: ‘Microphone‘
I want to get a YES by making the column value lower and because the IN function contains the value (‘microphone‘)
Thank you
You need to change the collation to make it case sensitive, by default it is case insensitive :
CASE WHEN REPLACE(producttitle, ' ', '') = 'microphone' COLLATE SQL_Latin1_General_CP1_CS_AS
THEN 'YES' ELSE 'NO'
END

SQL datatype BIT assign values

Can Bit be assigned values?
for example instead of
0=False, 1= true
it will be like below
0=outdoor 1=indoor
if able to then how do i assign the values to bit?
(SQL code)
No, Bit is a single bit, it has 3 (in MS SQL) possible states: 1,0 and NULL
The standard way to do this would be to have an 'Indoor' field, and use 1/0 as true/false.
In TSQL You could use a case statement to present this as indoor/outdoor at the database level:
select
case when indoor=1 then 'indoor'
else 'outdoor' end
as location
from table_name

Validation? Yes or No, otherwise Error. SQL

I'm trying to add to current query where a certain field name must contain either 'Y' or 'N'. I'm currently using substrings and isnumeric functions to manipulate the data within.
Here is an example:
(
LEN(STERLING_RETURN_SIGNAL) > 1
or ISNUMERIC(substring(STERLING_RETURN_SIGNAL,1,1)) = 1
)
So the STERLING_RETURN_SIGNAL, must either be 'Y' or 'N' otherwise +('Error message').
Many thanks. Using Sql Server Management Studio.
Please note, I am a beginner...
select
case
when
STERLING_RETURN_SIGNAL in ('Y','y','N','n')
then STERLING_RETURN_SIGNAL
else
'Invalid value for signal'
end as signal
from ...

Updating with case in SQL Server 2008 R2

I want to update a column according to another column value.
for example, In Value column i have numbers between 0 to 1.
I want to check values and if:
Values < 0.45 set ValueStatus=Bad
Values >=0.45 and values<0.55 set ValueStatus =SoSo
Values >= 0.55 set ValueStatus=Good
I wrote the query like this:
update table
set ValueStatus=(case
when Values<'0.45' then 'Bad'
when (Values>='0.45' and Values<'0.55') then 'SoSo'
when Values>='0.55' then 'Good'
else Values
end)
But i get this error :
Error converting data type varchar to float.
Type of Values is Float and ValueStatus is Nvarchar(50)
Thanks
try this (you were adding ' to the numbers and SQL takes them as varchar) :
update table
set ValueStatus=(case when Values<0.45 then 'Bad'
when Values>=0.45 and Values<0.55 then 'SoSo' when Values>=0.55
then 'Good' else Values end )
I believe your problem is based on how the case statement determines the return type. You can read about it here and here.
The numeric types have a higher precedence than the string types. With the else values, you have four clauses in the `case. Three return strings; one returns a number. The number trumps the types so it tries to turn everything into a number.
You can mimic this problem with:
select (case when 1=1 then 'abc' else 12.3 end)
Happily, you can fix this by removing the else clause which is not needed in this case.

How to return different strings from a Boolean type in MySQL?

If I have a column set up as Boolean in MySql, a query returns the value as either 0 or 1.
Is it possible to do something like this
SELECT `bool_value` AS "yes" OR "no"
What I mean is, return two different strings based on whether it is true or false.
SELECT CASE WHEN bool_value <> 0 THEN "yes" ELSE "no" END
You need the case statement.
SELECT (CASE WHEN column <> 0 THEN 'yes' ELSE 'no' END) As Value
http://dev.mysql.com/doc/refman/5.0/en/case.html
MySql supports the standdard SQL CASE statement, which other answers use. MySQL also has the shorter, but non-standard IF statement
SELECT IF(bool_value,'Yes','No')
See
MySQL Flow Control Functions: IF