How to set bool value in SQL - sql

I have a column which is bool. How can I set true, false values for that? Here is my query :
Update [mydb].[dbo].[myTable]
SET isTrue =
(
CASE WHEN Name = 'Jason' THEN 1
END
)
I don't know what to write after THEN keyword. Should I write 1 or true or 1 AS BIT or something else?

Sql server does not expose a boolean data type which can be used in queries.
Instead, it has a bit data type where the possible values are 0 or 1.
So to answer your question, you should use 1 to indicate a true value, 0 to indicate a false value, or null to indicate an unknown value.
Update [mydb].[dbo].[myTable]
SET isTrue =
CASE WHEN Name = 'Jason' THEN
1
ELSE
0
END

The query you added will work fine, but it will you have to take care of "FALSE" part as well otherwise it will try to enter NULL in your column.
Do you have any default value constrain on isTrue column?
Update [mydb].[dbo].[myTable]
SET isTrue =
(
CASE WHEN Name = 'Jason' THEN 1 ELSE 0
END
)

You need case statement with when and else if not any condition satisfied
Update [mydb].[dbo].[myTable]
SET isTrue = ( CASE WHEN Name = 'Jason'
THEN 1 else 0
END)

Use IIF function of sql server
DECLARE #a int = 45, #b int = 40;
DECLARE #a int = 45, #b int = 40;
SELECT IIF ( #a > #b, 'TRUE', 'FALSE' ) AS Result;
Result
--------
TRUE
(1 row(s) affected)
For Your Problem
Update [mydb].[dbo].[myTable]
SET isTrue = ( Name = 'Jason', 'TRUE', 'FALSE' )

Related

how to use declared variable to select data from another table in case when condition?

I made select query in which i want to select data based on condition.For this i declared one variable and set value of that variable in else part.I want to use that variable for further select in same else part how can i achieve this?Please Help
declare #stateid int
select CASE WHEN MstCustomerAddressInfo.StateId is null
THEN 24
ELSE set #stateid = MstCustomerAddressInfo.StateId
select mststate.statecode from mststate where MstState.StateId = #stateid
END AS StateCode
No, you can't have SET inside a CASE expression. Even you can't have multiple statements.
Same query you can write as following.
declare #stateid int
select CASE
WHEN MstCustomerAddressInfo.StateId is null THEN 24
ELSE
-- set #stateid = MstCustomerAddressInfo.StateId
(select mststate.statecode
from mststate
where MstState.StateId = MstCustomerAddressInfo.StateId)
END AS StateCode
from [Your_Table]

How to select BIT columns as TRUE

I have a table called TP_Roles.
This Table structure is:
Id PK, int, not null
Role_Name varchar(200), null
IsActive bit, null
How Do I insert bit value as True instead of 1 ?
Many thanks.
BIT values are 1/0 and they correspond to TRUE/FALSE accordingly.
By the comments I assume you want to view TRUE / FALSE when selecting this column, so you can simply use a CASE EXPRESSION for this:
SELECT <Column1>,<Column2>...,
CASE WHEN IsActive = 1 THEN 'TRUE' ELSE 'FALSE' END as IsActive
FROM YourTable
If IsActive = 1 - display TRUE , else , display FALSE.

how to get "AND" of all the values of a boolean column in a single sql

how to get "AND" of all the values of a boolean column in a single sql query.
for above the o/p should be false,
if all were true then o/p: true,
if all/atlease one value false , then o/p: false.
Try this
IF EXISTS (SELECT ActiveStatus From TableName where ActiveStatus = 0)
SELECT 'False'
ELSE
SELECT 'True'
AS OutputColumn
You can indirectly use MIN for AND, and MAX for OR.if you take into account that a BIT colum is either zero or one:
MIN: only if all the values are 1 (true) the result will be 1 (true), it works like AND
MAX: if there is at least a 1 (true) the result will be 1 (true), it works like OR
If you try to use MIN or MAX directly on a BIT column, it will fail, so you need to cast it to an integer type, and back to bit, like this:
SELECT CAST(MIN(CAST(BitColumn AS TINYINT)) AS BIT) FROM Table -- for AND
SELECT CAST(MAX(CAST(Bitcolumn AS TINYINT)) AS BIT) FROM Table -- for OR
It's easy to include this in a more complex query, for example one with GROUP BY
declare #status varchar(10)
if exists(select null from [Status] where ActiveStatus = 0)
set #status = 'false'
else
set #status = 'true'
select #status as [Status]

Using ISNULL in where Clause doesn't return records with NULL field

Table:
ID AppType AppSubType Factor
1 SC CD 1.0000000000
2 SC CD 2.0000000000
3 SC NULL 3.0000000000
4 SC NULL 4.0000000000
Query:
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and AppSubType = ISNULL(#ast, AppSubType)
Result:
ID AppType AppSubType Factor
1 SC CD 1.0000000000
2 SC CD 2.0000000000
Question:
Shouldn't this query return all 4 records and not just the first 2?
Abviously #ast is null and Isnull would exchange null with other value, so you shouldn't expect #ast to be not null. If your AppSubType is null , so the result become null but AppSubType=null doesn't mean because AppSubType is null is true. Because null is not a value so it cant work with equal.
for your expected result this code will work.
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and (AppSubType = ISNULL(#ast, AppSubType) Or AppSubType is null)
You can write a case condition in where clause as:
declare #ast varchar(10)
set #ast = null
select *
from tbl
where AppType = 'SC' and 1=
case when isnull(#ast ,'') = '' and isnull(AppSubType ,'') = '' then 1
when AppSubType = ISNULL(#ast, AppSubType) then 1
else 0
end
Please understand the behavior of ISNULL explained in below blog.
the very first expression in the isnull function is a column value or the expression of some result.
ISNULL Explored in Detail
The code looks like a search functionality. If the value is not given then replace them with whatever is there in database and if given pull only those records which are matched.

How to generate Null or int in a column?

I need to fill null or int randomly in an int field.
How ??
OK, this may be a partial answer but...
SELECT CASE WHEN RAND() > 0.5 THEN 'YourInt' ELSE NULL END
Will give you either 'YouInt' or NULL based on a random value.
You could then perform an INSERT like this...
INSERT INTO MyTable
SELECT CASE WHEN RAND() > 0.5 THEN 'YourInt' ELSE NULL END
Or, of course an UPDATE
UPDATE MyTable
SET MyField = CASE WHEN RAND() > 0.5 THEN 'YourInt' ELSE NULL END
WHERE MyCondition = MyCriteria
But it does not generate an INT for you. Do you already have some INT's somewhere or do you need to generate them?