Column boolan to value GUI - sql-server

I cant seem to find a answer to this. I am using GUI to create a table and a column in there is called DuesPaid. I need this column boolan value to be false and it has the datatype of bit. How might one do this am I just missing where its at?

BIT is the type to store boolean values. 1 will mean true, and 0 will mean false. You can also compare it to strings 'true' and 'false'.
If you run this query:
SELECT IIF(CAST(1 AS BIT) = 'true', 'Yes', 'No') as [1 = true?]
, IIF(CAST(0 AS BIT) = 'false', 'Yes', 'No') as [0 = false?]
, IIF(CAST(0 AS BIT) = 'true', 'Yes', 'No') as [0 = true?]
You'll get the following:
1 = true? 0 = false? 0 = true?
--------- ---------- ---------
Yes Yes No

Related

SQL Server, If value exists on my ID List, increment all the values required

I have this:
IF (#Orden EXISTS IN (SELECT A.Orden
FROM PCC.ARTICULOS A
INNER JOIN PCC.RLArticulos_Categorias AC ON (A.Id_Articulo = AC.Id_Articulo)
INNER JOIN PCC.Categorias B ON (AC.Id_Categoria = B.Id_Categoria)
WHERE AC.Id_Categoria = #Id_Categoria))
#Orden is a value selected by the user (Example: 1, 2, 3, 4)
#Id_Categoria is a static value such as (Example: 1, 2, 3 ,4)
Test:
In this test I will use #Id_Categoria = '2' , and #Orden = '2'.
What do I want?
In my select statement I get (1,2,3,4), so I need to verify if value '2' (#Orden) exists on that list (1,2,3,4) If this is true, i should ADD for 2,3,4 (+1) value. So #Orden '2' can be inserted later.
Could you please help me?

Confusing null compares combined with NOT

I realize that comparing NULL to any other value (including NULL) will always result in false.
DECLARE #IsSet bit = NULL;
SELECT IIF(#IsSet = 1, 'true', 'false')
SELECT IIF(#IsSet != 1, 'true', 'false')
This outputs:
false
false
But this is part that confuses me:
SELECT IIF(NOT(#IsSet = 1), 'true', 'false')
SELECT IIF(NOT(#IsSet != 1), 'true', 'false')
This also outputs:
false
false
I would expect that the NOT would have flipped the value to TRUE. (Which it does if #IsSet is set to 0 for the first expression)
It seems that the compare to the null value has some power over the boolean logic outside the parenthesis.
But the null compare is not all powerful over boolean logic:
SELECT IIF((#IsSet = 1) OR (1=1), 'true', 'false')
SELECT IIF((#IsSet != 1) OR (1=1), 'true', 'false')
This returns:
true
true
I don't understand what is happening here, but I assume that this is done on purpose. But I don't know why.
Can someone explain why NOT(NULL!=1) does not equal true.
A comparison with NULL results in UNKNOWN rather than TRUE or FALSE. NOT UNKNOWN also results in UNKNOWN, which is neither TRUE nor FALSE. One cannot "flip" UNKNOWN to a Boolean value using NOT.
This 3-way logic requires one to use IS NULL or IS NOT NULL to test for NULL values rather than traditional Boolean logic.
The way you are using NOT is not correct. You'll get error, If you only execute the NOT condition as follows:
SELECT NOT(#IsSet = 1)
When you enclose your incorrect usage of NOT condition inside IIF condition, SQL server won't show you the error, However the statement will be evaluated to false output.
If you want to explicitly check for NULL value, then following practice can be adopted.
SELECT IIF(#IsSet IS NULL, 'true', 'false')
Lastly, the following condition is returning 'true' in output, because one of the OR condition (1==1) is always evaluating to 'true', hence the overall output of the IIF statement is true.
SELECT IIF((#IsSet = 1) OR (1=1), 'true', 'false')
SELECT IIF((#IsSet != 1) OR (1=1), 'true', 'false')

Update table with SET command IF / ELSE

I'm trying to update a table and i want to runt 2 different SET senarios depending on a stock value.
Working CODE that does one senario.
UPDATE dbo.ar
SET webPublish = '0',
ArtProdKlass = '9999',
VaruGruppKod = '9999',
ItemStatusCode = '9' --Utgått ur sortimentet+
FROM tmp_9999
WHERE ar.ArtNr = tmp_9999.art AND ar.lagsadloartikel < '1'
What i would like to do is that IF last statement (ar.lagsaldoartikel) is >'1'Then i would like it to run this SET:
SET webPublish = '1',
ArtProdKlass = '1',
VaruGruppKod = '9999',
ItemStatusCode = '8'
So something like this i have tested:
IF AR.lagsaldoartikel < '1'
SET webPublish = '0',
ArtProdKlass = '9999',
VaruGruppKod = '9999',
ItemStatusCode = '9' --Utgått ur sortimentet+
FROM tmp_9999
WHERE ar.ArtNr = tmp_9999.art --Väljer ut artiklar som enbart finns i textfilen och har lagersaldo mindre än 1
ELSE
SET webPublish = '1',
ArtProdKlass = '1',
VaruGruppKod = '9999',
ItemStatusCode = '8' --Utgått ur sortimentet
FROM tmp_9999
WHERE ar.ArtNr = tmp_9999.art --Väljer ut artiklar som enbart finns i textfilen och har lagersaldo mindre än 1
Using CASE:
UPDATE dbo.ar
SET webPublish = '0',
ArtProdKlass = '9999',
VaruGruppKod = '9999',
ItemStatusCode = CASE WHEN AR.lagsaldoartikel < '1' THEN '9' ELSE '8' END
FROM tmp_9999
WHERE ar.ArtNr = tmp_9999.art
(If ItemStatusCode et al are numeric you should treat them as such.)
If is part of the procedural T-SQL. You can't use procedural statements inside the relational ones - the only way to use if would be to have two separate update statements, each in one branch of the if. However, that's a bad idea - it's not concurrency-safe.
One way to accomplish what you're trying to do is to use the case statement instead - that's just an expression, so it can be used in the set clause just fine:
set webPublish = case when AR.lagsaldoartikel < '1' then '0' else '1' end
(etc. for the other arguments).
However, I'd like to warn you - this is almost certainly a bad idea. It's probably going to back-fire on you soon in the future, when you realize that there's ten different conditions and a hundred different possible values you might want. Consider using a more idiomatically relational way of doing this - for example, taking the conditions and arguments from a different table - it's not necessary now, but if you ever find your conditions are expanding out of reasonable size, remember to consider changing the whole structure of the command if needed.

Set operations in DBIx::Class

What is the best way to perform set operations using DBIx::Class?
I saw that one solution would be to create a Result Source based on my query, but my conditions will be defined by the user and I don know if the best answer is to create the result source on the fly.
Basically i need to translate this type of query to DBIC where code, attr_name and value is defined by the user:
SELECT pid FROM product WHERE code = 48
INTERSECT
(
( SELECT pid FROM attr WHERE attr_name = 'color' AND value = 'blue'
INTERSECT
SELECT pid FROM attr WHERE attr_name = 'size' AND value = 'big'
)
UNION
( SELECT pid FROM attr WHERE attr_name = 'color' AND value = 'green'
INTERSECT
SELECT pid FROM attr WHERE attr_name = 'size' AND value = 'small'
)
)
Could DBIx::Class::Helper::ResultSet::SetOperations be what you need?

how can I have different where condition using case statement?

#HistoryMSBType => This is a variable which can contain any varchar.
Depending upon it's value, i need to have different type of where clause.
How can I achieve this ?
SELECT * FROM stageTable map
WHERE id = 1
CASE #HistoryMSBType
WHEN 'Utilization Other' THEN
AND map.ColumnID = 4
WHEN 'Cost Other' THEN
AND map.ColumnID = 6
ELSE
AND map.ColumnName = #HistoryMSBType
END
SELECT *
FROM stageTable map
WHERE id = 1
AND (
(#HistoryMSBType = 'Utilization Other' AND map.ColumnID = 4)
OR
(#HistoryMSBType = 'Cost Other' AND map.ColumnID = 6)
OR
(isnull(#HistoryMSBType,'') NOT IN ('Utilization Other','Cost Other')
AND map.ColumnName = #HistoryMSBType)
)
You need the ISNULL to make it match the CASE-ELSE exactly, but it won't matter if it can never be null.

Resources