Incompatible data types in CASE STATEMENT - sql

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.

Related

How can I skip the then part of a case statement in SQL

I have a situation in which I need to do nothing if these conditions are met in the when part and assign value otherwise. I am using the case when <expression> then <some operation/assignment> end in my SQL.
I want to skip this then part as this is a special condition and I do not want any assignment here just pass by to the else
(case
when '$usertype_id' = 'null' or '$usertype_id' = 0 or '$usertype_id' = 999
then //do nothing
else x.usertype_id = '$usertype_id'
end)
Please let me know how can I achieve this. Thank you.
You don't. You use conditional logic without a case expression:
('$usertype_id = 'null' or '$usertype_id = 0 or '$usertype_id = 999) or
(x.usertype_id = '$usertype_id')
More importantly, I would strongly, strongly encourage you to use parameters instead of munging query strings with parameter values.

Value seems fine, but comparing to expected value using = returns false

I have a varchar column, set as varchar(255), but i cannot query using an = operator.
I know there is data in this set where the field (UOM) = 'PK', but when i query this i get no results. If i query UOM LIKE '%PK%', i get results, but not using a straight equal operator. I have tried changing the datatype to nvarchar, and also tried seeing if there were spaces in the columns throwing it off, but no luck.
Has anyone run into anything like this, and how did you solve? Could the column be corrupted?
Thanks for the replies all! I found this helpful article which showed some hidden ASCII characters in the field. A quick replace statement and we're back to functioning! Thanks again everyone for the quick replies.
Most likely a leading space... try where ltrim(UOM) = 'PK'. Most often, trailing spaces do not affect equality operations, but you could also do where ltrim(rtrim(UOM)) = 'PK'. I wouldn't expect there to be a case sensitivity issue, but keep that in mind when comparing strings as well, and this is where you may want to use the UPPER() or LOWER() methods.
Next, you'll want to start looking for carriage returns, line feeds, tabs, etc.
declare #var varchar(64) = char(10) + --LF
char(13) + --CR
'PK'
select
case when replace(replace(#var,char(10),''),char(13),'') = 'PK' then 1 else 0 end
,case when #var = 'PK' then 1 else 0 end
Naturally, you could just clean your data if this is the case, or continue to us LIKE

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

Replacing a calculated field column with a Blank when a NULL is returned?

I'm doing a simple query that uses the DateDiff function to find the number of days between the dates. However, with regards to certain instances, I'd like to populate a blank field (not a null).
Something like this is what I currently have, and it seems to work fine (but it populates a null).
[Test (Years)] = CASE WHEN TYPE IN ('A','B')
THEN NULL ELSE IsNull(CONVERT(decimal(28,12),
(DATEDIFF(d,#StartDate,ExpirationDate)))/365,0) END
Now if I try something like this... which tries to convert all TYPE A and B to populate a blank, I'll get the following error message: Error converting data type varchar to numeric.
[Test (Years)] = CASE WHEN TYPE IN ('A','B')
THEN '' ELSE IsNull(CONVERT(decimal(28,12),
(DATEDIFF(d,#StartDate,ExpirationDate)))/365,0) END
Is there a simple thing I'm missing? I've tried doing the calcualtions without converting to a decimal, but it doesn't seem to work. Any ideas? Thanks
CASE is an expression that returns exactly one value and all of the branches must yield compatible types. A string (even a blank string) is not compatible with a decimal, so you need to do something like:
CASE WHEN ... THEN '' ELSE
CONVERT(VARCHAR(32), COALESCE(CONVERT(DECIMAL(23,12), ... ,0)) END
Note that this hack will only work if you are presenting the data to an end user. If you are trying to store this data in a column or use it in other calculations, it too will be tripped up by the blank string. A number can't be a blank string:
DECLARE #i INT = '';
SELECT #i;
Result:
0
So, if you don't want "empty" numerics to be interpreted as 0, stop being afraid of NULL and if you are dealing with this at presentation time, have the presentation layer present a blank string instead of NULL.

Add True Value to Case Statement

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