trouble with WHEN NOT NULL - sql-server-2008

I want to create a switch around the following. I've tried various permutations but I just cant get IS NOT NULL to work.
(CASE billing_code WHEN NOT NULL THEN billing_amount END) AS Billing Amount
Thanks in advance

You need to use the "searched" form of the CASE statement. Additionally as the column alias contains spaces it needs to be delimited as below.
CASE WHEN billing_code IS NOT NULL THEN billing_amount END AS [Billing Amount]

Try as below
(Case When billing_code is Not Null then billing_amount End) As "Billing Amount"

Related

SQL CASE Statement Error while checking conditions

The scenario is that i have two columns one is Quantity and other is Type. Now what i am trying to do is check if type is "rec" then it take all the values from quantity and add them and if the type is "issue" then it will get only those fields whose type is receiving and add them all on the basis of ITEM ID. The SQL Query i have written is here:
SELECT f.`Itemm_ID`,ABS(SUM(f.`Quantity`)) AS recieving, TYPE ,
(CASE
WHEN f.`Type` = 'issue'
THEN ABS(SUM(f.`Quantity`))
END)
FROM stock_journal AS f
WHERE f.`Itemm_ID`='1'
Now the thing is everything is working fine except CASE statement which is returning null.
Please help me in resolving my issue. Thank you
It seems that you need in
SELECT f.`Itemm_ID`,
ABS(SUM(f.`Quantity`)) AS recieving,
TYPE,
ABS(SUM(CASE WHEN f.`Type` = 'issue'
THEN f.`Quantity`
ELSE 0
END))
FROM stock_journal AS f
WHERE f.`Itemm_ID`='1'
PS. Does f.Quantity may be negative? If not then ABS() is excess. If it may then ABS() must wrap inner f.Quantity, not the whole SUM(), maybe.
PPS. TYPE in output is formally incorrect (contradicts with ONLY_FULL_GROUP_BY), I'd recommend wrap it with ANY_VALUE().
i didn't get your recommendation of wrapping type with value can you please elaborate more.
I mean that (maybe, I'm not sure) you need
SELECT f.`Itemm_ID`,
SUM(ABS(f.`Quantity`)) AS recieving,
TYPE,
SUM(CASE WHEN f.`Type` = 'issue'
THEN ABS(f.`Quantity`)
ELSE 0
END)
FROM stock_journal AS f
WHERE f.`Itemm_ID`='1'
Have you checked syntax for CASE I think you are missing ELSE part in the query
Eg:-
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN "The quantity is greater than 30"
WHEN Quantity = 30 THEN "The quantity is 30"
ELSE "The quantity is under 30"
END
FROM OrderDetails;
check here for syntax

MySQL compare different values in case when function

In this query I use the CASE WHEN function to compare variables values in two different conditions. The problem is the number, because return a value = 1, but the correct result is NULL (20 is > of 8).
SELECT DISTINCT
case
when '20'>= '8' then null
when '20'< '8' then '1'
ELSE '0'
end checkValue
FROM DUAL;
How to resolve this problem?
The query run correctly if change 8 with 08. This solution is not applicable to because from variable arrive numers: 10 100 1000 units.
Thanks
I have change the field type from VARCHAR to DECIMAL and in this case work fine.
if you want compare string remember that string '20' < '8' so 'AB' is < 'C'
SELECT DISTINCT
case
when '20'>= '08' then null
when '20'< '08' then '1'
ELSE '0'
end checkValue
FROM DUAL;
otherwise if you want a number compare then you shoudl convert tne string as number
You need just cast strings to some numeric data type and then compare, for example
SELECT DISTINCT
case
when cast('20' as signed)>= cast('8' as signed) then null
when cast('20' as signed)< cast('8' as signed) then '1'
ELSE '0'
end checkValue
FROM DUAL;
Side note: DISTINCT no makes sense in this particular case

Case statement for a date field with Mysql db

Iam trying to do a CASE statement. The requirement is if Date field is NULL then show it as 'Pending' or else show the date from the db.
I have tried the below case statement but there is some problem.
CASE po_amend_received_date IS NULL WHEN 1 THEN 'Pending'
ELSE po_amend_received_date END AS `po_amend_received_date`
Am i making any mistake here?
CASE WHEN po_amend_received_date IS NULL THEN 'Pending'
else po_amend_received_date
end as `po_amend_received_date`

CASE WHEN statement with multiple IS NULL conditions

I have 3 variables(sedol, cusip, isin). I want the to pull in the SEDOL if it is available, if not then pull in CUSIP. If CUSIP is also unavailable then pull in ISIN.
Below is the code I've written. The problem is that when CUSIP and SEDOL is unavailable it does not pull in the ISIN. I can't figure out where I may have missed something.
CASE
WHEN sedol IS NULL THEN cusip
WHEN cusip IS NULL AND sedol is NULL THEN isin
ELSE sedol
END
Appreciate the help!
Try this:
COALESCE(SEDOL, CUSIP, ISIN)
The order of your operations are preventing the 2nd when condition from ever being executed. You would need to swap the when conditions.
You want to look into the coalesce function.

Using Sum inside a Case statement in MySQL

I am having problem in MySQL adding three values, this should be simple right?
I have code that selects values from a column based upon the value of a second column, I use a case statement like this:
Select
Max(Case
When Table1.costcode Like '%Costcode1%'
Then Table1.costs
Else Null End) As 'Costcode1',
Max(Case
When Table1.costcode Like '%Costcode2%'
Then Table1.costs
Else Null End) As 'Costcode2',
Max(Case
When Table1.costcode Like '%Costcode3%'
Then Table1.costs
Else Null End) As 'Costcode3',
(Case
When Table1.costcode In ('%Costcode1%','%Costcode2%','%Costcode3%')
Then Sum(Table1.costs)
Else Null End) As 'Total Cost',
From Table1
the first three Case statements work fine and all return values (these are held in the database as negative numbers e.g. -13624.00), however the Total Cost Case just returns Null...
The column Table1.costcode includes many other codes as well so I can't just sum all of the values without picking them out first.
It must be simple to sum these values, but obviously I'm missing something… Help, please :-)
Thanks
Try this -
SUM(Case Table1.costcode
When LIKE ('%Costcode1%') Then Table1.costs
When LIKE ('%Costcode2%') Then Table1.costs
When LIKE ('%Costcode3%') Then Table1.costs
Then Table1.costs
Else 0.00 End) As 'Total Cost'