Add True Value to Case Statement - sql

CASE
WHEN VPN_Access__c = True THEN 'Need to Setup' + VPN_Access__c
ELSE ''
END AS VPNAccessDesc`
I'm trying to create a Case statement but I keep getting an error stating invalid column name 'True'. It's a checkbox field and I want it to say "Need to Setup" if the table equals True.
VPN_Access__c is a bit datatype if that makes a difference

Sql Server doesn't have boolean values, so you'll need to do:
CASE WHEN VPN_Access__c = 1 THEN 'Need to Setup'
ELSE '' END AS VPNAccessDesc
Actually - you can remove + VPN_Access__c as it makes more sense to say Need to Setup rather than Need to Setup1

I'm not sure which database you're using, but in mysql5, BIT isn't the same as BOOLEAN, it's a bit field, you can't reliably compare it to true/false

Related

SQL Working with Bit values to get either true or false

I have a SQL table that has a bit column named "Split".
I need to create a stored procedure that receives a parameter named WithSplit of type bit.
If WithSplit is false then I need to get all the records that have the "Split" column equal to false.
If WithSplit is true, then I need to get the records having the "Split" column value either true or false.
That is , if WithSplit is false then get only the records where split=withsplit, else get all records.
How should this be achieved?
I have found the answer:
(Split = #WithSplit or #WithSplit = case when #WithSplit =1 then 1 end)

Setting BIT variable using CASE statements

I'm in the middle of a migration that uses some reporting elements from Crystal and attempting to convert what existed previously into SQL. It looks to use VB scripting but I can't figure out how to make the switch over.
Initially I'd assume a CASE WHEN statement would suffice but I can't determine the right logic behind the query.
Sample of the VB below:
Dim HasValue As Boolean
If isnull({Reference.Shallow}) Then
HasValue=False
formula="MISSING"
Else
HasValue=True
End If
I am aware that IF does exist in SQL Server but when researching it, people tend to stray away from it as CASE WHEN does the same?
I have some psuedo that I'd imagine should work in the same way but this does not resolve as you cannot set a variable within a CASE WHEN statement (I believe):
DECLARE HasValue BIT
CASE WHEN Reference.Shallow IS NULL
THEN SET #HasValue = 1
ELSE SET #HasValue = 0
END AS Shallow
What would be the most appropriate way of doing this within SQL?
You must use this code snippet
DECLARE #HasValue BIT
SELECT #HasValue=CASE WHEN Shallow IS NULL THEN CAST(1 AS BIT) ELSE CAST(0 AS
BIT) END FROM Reference.Shallow
CASE returns a scalar value, not a operation or boolean result. You set the value of the variable to the result of the CASE:
SET #HasValue = (SELECT CASE WHEN R.Shallow IS NULL THEN 1 ELSE 0 END
FROM dbo.Reference R
/*WHERE...? */);

Incompatible data types in CASE STATEMENT

I am converting a SAS code into Cognos Report Studio version 10 where am stuck at a point where there is data formatting needed.
IF SDW_STAT ^= '' THEN PRINCIPAL_BAL = TOT_PRIN;
Where '' represnts single quotes without any space
I tried writing a CASE STATEMENT:
CASE
WHEN ([SDW_STAT] IS NOT MISSING) THEN ([PRINCIPAL_BAL] = [TOT_PRIN])
ELSE ('')
END
I also tried couple of options like <> '', is not null, <> ' ' instead of IS NOT MISSING but none of them worked. Can you please suggest where I am going wrong?
Pls note: SDW_STAT column has few blank fields and some characters like 'Y' 'S' etc.
Try putting the column outside of the CASE statement:
PRINCIPAL_BAL =
CASE
WHEN COALESCE(SDW_STAT,'') <> '' THEN TOT_PRIN
ELSE PRINCIPAL_BAL
END
Logically, this will update the principal_bal to tot_prin when sdw_stat doesn't equal blank or null. If it is blank, then it will just update it to itself.
The problem is likely the ELSE statement. Your THEN clause is an equation. This will return a boolean, either true or false. Your else is returning a string. These types aren't compatible. You can test this by changing your ELSE to:
ELSE (false)
My guess is the data type error will go away.
Can you elaborate on where you are using this code? Is it in a data item or in a filter? There are different rules depending on where your expression is used.
Note: I would comment on the question directly but I'm new and don't yet have the ability.

db2 stored procedure if else variable comparison

I am trying to compare a variable in db 2 sp using like, however it always goes to the else part of the statement, can someone correct the syntax here..here is the part of the code
do
IF(#variable like '%abc') THEN
set #anotherVariable='abc';
ELSEIF (#variable like '%def') THEN
set #anotherVariable='def';
ELSEIF (#variable like '%def') THEN
set #anotherVariable='def';
ELSE
set #anotherVariable='xyz';
END IF;
END FOR;
This code is part of a cursor, query always returns 1 value, however my comparison is not working(incorrect syntax?), it always goes to the last else as if it never was able to match. I know that value is there but its not comparing in this manner...Thanks
Are you writing SQL-PL code? Because variables are referenced directly by its name, not preceded by the '#' sign.
Another thing is that you are using the like operator outside a query. 'Like' is not a function, it is a predicate: http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000751.html
Instead, you can use a 'case' in the select and then return the corresponding value. In that way you do not need to do an 'if-else': http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0023458.html

Why can't I use a bit field as a boolean expression in a SQL case statement?

I want to select something conditionally based on whether a bit field is true or false. This was the syntax that I originally tried:
CASE WHEN isSoon THEN 'Soon' ELSE 'Not so soon' END As HowSoon
This makes sense to me since what follows the "WHEN" has to be a boolean expression, which isSoon is, as it's a bit field. However, this didn't work. What I had to do in the end was:
CASE WHEN isSoon = 1 THEN 'Soon' ELSE 'Not so soon' END As HowSoon
This seems redundant to me... It's like writing if(isSoon == True) in a programming language instead of the more intuitive if(isSoon) and goes against the grain. Why is SQL set up like this? Is it because bit fields aren't truly boolean?
Because the bit datatype is not a boolean type, it's a datatype used to optimize the bit storage.
The fact that the string "true" and "false" can be converted to a bit can be misleading, however, quoting from MSDN , a bit is "An integer data type that can take a value of 1, 0, or NULL."