How to write case in sql server? - sql

In sqlserver, I write a query in that i use "case" but it is giving error, this is my case.
(case when sm.SegCode =0 then '' else sm.SegCode = 7 end)
please help me.
Thanks for all for giving response, actually I have a parameter #id. Now I want to check when it is not zero I check that condition sm.segcode else if #id is zero then I don't want to check the condition that is sm.segcode = #id.

There are two issues with the statement you showed:
the syntactic error of else sm.SegCode = 7
the attempted mixing of types with the empty string and the int
Try this instead:
case when sm.SegCode = 0 then '' else '7' end
Of course that is partially a guess, because I'm not sure exactly what you are trying to achieve by setting the result to either an empty string or the integer 7.

this would be syntactically correct:
case sm.SegCode when '0' then '' else '7' end
or
case sm.SegCode when 0 then NULL else 7 end
if that columns allows NULL's that is
you can see more about case when in the documentation: http://msdn.microsoft.com/en-us/library/ms181765.aspx

Always try to return Same DATA TYPE value from Case statement, seems your first condistion returns VARCHAR whereas the else section returns INT, it's not possible, so best to convert each value to VARCHAR, so 7 will be '7' in else statement, thanks
(CASE
WHEN sm.SegCode = 0 THEN ''
ELSE '7' END)

Related

Using function inside CASE

I am trying to use the SUBSTR and NVL functions inside the case. The case is in the where clause of the select statement.
The code below gives the following error:
ORA-00905: missing keyword
AND ( CASE
WHEN SUBSTR(upper(p_open_invoice),1,1) = 'Y' THEN
NVL(P.AMOUNT_DUE_REMAINING,0) = 0
ELSE
1=1
END)
This looks like a syntax error around equal operator of NVL function.
That is not how case expressions work (in Oracle) -- there is no boolean type to return.
The simplest method is to remove the `case and express this as simple logic:
AND (SUBSTR(upper(p_open_invoice), 1, 1) <> 'Y' OR
COALESCE(P.AMOUNT_DUE_REMAINING, 0) = 0
)
If p_open_invoice can be NULL, you need to take that into account as well.
You cannot use a collation as a result for case..when statements, it's better converting the condition to
AND (( SUBSTR(upper(p_open_invoice),1,1) = 'Y' AND NVL(P.AMOUNT_DUE_REMAINING,0) = 0 )
OR SUBSTR(upper(p_open_invoice),1,1) != 'Y' )
If you're accustomed to programming in PL/SQL you may have seen that there's a BOOLEAN type in PL/SQL. However, this is not true in the Oracle database itself. The way I usually work around this is to use character expressions which return 'Y' or 'N' instead of TRUE or FALSE.
Keeping this in mind - if you really want to use a CASE expression similar to what you had originally you can use the following:
AND CASE
WHEN SUBSTR(upper(p_open_invoice),1,1) = 'Y'
THEN CASE
WHEN NVL(P.AMOUNT_DUE_REMAINING,0) = 0 THEN 'Y'
ELSE 'N'
END
ELSE 'Y'
END = 'Y'
Here the CASE expression returns either 'Y' or 'N', which is then compared with 'Y'.

Use case statement in SQL

I want to change the value in a result using a case statement, but it seems to ignore this. Very simple, if the value in the table is 1 then must change to True, if the value is 0 then must change to false. Here is what I did:
CASE pbc.bShownUp
WHEN 0 THEN 'False'
ELSE pbc.bShownUp
END
AND
CASE ch.bShownUp
WHEN 1 THEN 'True'
ELSE ch.bShownUp
END
No matter what I do, the result is still 0 or 1.
Please help.
When you're using a CASE Expression, the return type of the data has to be the same for all WHEN parts or more simply put, CASE must resolve to a single type.
More reference here and here
From a Type point of view, at the moment your code says
CASE BIT
WHEN 1 then NVARCHAR
ELSE BIT
And that won't work.
So you have to do something like this
CASE ch.bShownUp
WHEN 1 then 'TRUE'
ELSE 'FALSE'
If bShownUp is numeric column, you are producing mixed result - sometimes 0/1, sometimes 'False'/'True' e.g. string. I suppose bShownUp is of type VARCHAR and you have to change WHEN 0 to WHEN '0'.
If your bShownUp is VARCHAR then your query should look like
CASE pbc.bShownUp
WHEN '0' THEN 'False'
ELSE pbc.bShownUp
END
AND
CASE ch.bShownUp
WHEN '1' THEN 'True'
ELSE ch.bShownUp
END
OR
Try something like below,
CASE pbc.bShownUp
WHEN 0 THEN 'False'
ELSE CAST(pbc.bShownUp AS VARCHAR(10))
END
AND
CASE ch.bShownUp
WHEN 1 THEN 'True'
ELSE CAST(ch.bShownUp AS VARCHAR(10))
END
I think this will do:
case ch.bShownUp
when 1 then 'True'
else 'False'
end

SQL: How to replace field when 0 and return same data when it's not?

I have a column that is returning either nothing, 0 or Budget codes.
The column is ExerciceBudgetaire and I want to replace 0 by '' (empty field) and keep other fields as they are: if 0 then '', if not, return ExerciceBudgetaire
Here is what I did, but it's not working:
Select DescriptionLigne,ref1
case ExerciceBudgetaire
WHEN 0 THEN ''
else ExerciceBudgetaire
end
from dbo.CODA
Thanks for your help!
Try this
Select DescriptionLigne,ref1, case when ExerciceBudgetaire = 0 THEN '' else ExerciceBudgetaire end from dbo.CODA
(Assuming ref1 to be a column)
There's one issue where you were missing a comma, which both current answers address.
The second issue is that
case ExerciceBudgetaire
WHEN 0
Has a string value coming from ExerciceBudgetaire and a constant int value, 0. int has a higher type precedence than any of the (n)(var)char types, and so it tries to convert all of the strings to ints to see if they're 0. So, instead, pass it a string constant instead:
Select DescriptionLigne,ref1, --New comma
case ExerciceBudgetaire
WHEN '0' THEN ''
else ExerciceBudgetaire
end
from dbo.CODA
(Both of the current answers also switch the CASE expression from a simple CASE expression to a searched one. However, I see no necessity to do that)
SELECT
DescriptionLigne
,ref1
,CASE WHEN ExerciceBudgetaire = '0'
THEN 'YOUR VALUE TO REPLACE'
else ExerciceBudgetaire
end AS ExerciceBudgetaire
from dbo.CODA

TSQL Comparing 2 uniqueidentifier values not working as expected

I'm trying to compare 2 uniqueidentifier values as shown in the query below. However, if one value is null and one value isn't, the result is 'same'?! I'm sure that both values are uniqueidentifiers, and have also tried casting both values to uniqueidentifier to make absolutely sure. The 2 values being compared are coming from different databases with different collations. Does the collation make any difference? Any ideas would be appreciated.
select [result] = case when
[target].StaffID <> [source].StaffID then 'different'
else 'same'
end
from
...
If I replace the <> with an = the query then thinks that 2 null values don't match.
EDIT:
I used:
declare #empty uniqueidentifier
set #empty = '00000000-0000-0000-0000-000000000000'
... isnull(somevalue, #emtpy) <> isnull(othervalue, #empty) ...
NULL is neither equal to something nor equal to nothing. Generally you'd check for null values by comparing with IS NULL. For example,
somefield IS NULL
You could look into using COALESCE for what you're trying to do -- just make sure you use the same data types (in this case UniqueIdentifier):
...
case
when coalesce(t.StaffID,'00000000-0000-0000-0000-000000000000') <>
coalesce(t2.StaffID,'00000000-0000-0000-0000-000000000000')
then 'different'
else 'same'
end
...
http://sqlfiddle.com/#!3/181e9d/1
null is more of an unknown, it's not really a value. Unfortunately SQL will tell you that null = null is false.
Basically you have to cast nulls to empty strings than you can compare. We use IFNULL(value, replacement) to do that...
http://msdn.microsoft.com/en-us/library/ms184325.aspx
Hope this helps.
select case when null = null then 'equal' else 'not equal' end
Above will be "not equal"
select case when ISNULL(null, '') = ISNULL(null, '') then 'equal' else 'not equal' end
This one will be "equal"
And finally your case...
select [result] = case when
ISNULL([target].StaffID, '') <> ISNULL([source].StaffID, '') then 'different'
else 'same'
end
from

SQL CASE statement returns error

Hi I have select CASE statement in DB2:
case when actualfinish is not null then dec (( timestampdiff(
4,
char(actualfinish - reportdate))/60.00),10,2)
else 'not'
end
It returns me error.
If I delete ELSE part there is no error and calculation is done.
case when actualfinish is not null then dec (( timestampdiff(
4,
char(actualfinish - reportdate))/60.00),10,2)
end
If I change THEN part to something like
case when actualfinish is not null then 'Yes'
else 'not'
end
also there is no error.
But for my complete SQL query - can't find an error.
Thanks
Issue here is that dec function returns a decimal datatype in the else part of the CASE statement you are returning a VARCHAR, hence the issue.
In your second version
case when actualfinish is not null then 'Yes'
else 'not'
end
Both the when and else are returning same datatype hence the query runs fine.
Change your else to send a decimal equivalent value and it should execute fine.
Update:
Use this version if you can return decimal as a string:
CASE
WHEN actualfinish IS NOT NULL THEN CHAR(DEC((TIMESTAMPDIFF(4, CHAR(actualfinish - reportdate))/60.00),10,2))
ELSE 'not'
END