This question already has answers here:
CASE vs. DECODE
(7 answers)
Closed 6 years ago.
I need to convert the below decode to Case statement in SQL. Tried multiple ways , not able to get it right.
select
DECODE(SIGN(A.column - to_date((
DECODE('10/01/2011',
'%',to_char(A.column,'mm/dd/yyyy'),'10/01/2011') ),
'mm/dd/yyyy')),
-1, 0,
A.Amount))
from A
select case
when to_date(nullif(:dt,'%'),'mm/dd/yyyy') > A.column
then 0
else A.Amount
end
from A
The best approach to handle such code is to remove it and find the original requirement.
I suspect it was such as
1) if '%' is passed return AMOUNT
2) if a date string is passed return AMOUNT if the COLUMN is greater or equal than the parameter
3) return 0 otherwise
This leads to following CASE statement
select A."COLUMN",
case when :1 = '%' then A.Amount
when A."COLUMN" >= to_date(:2,'mm/dd/yyyy') then A.Amount
else 0 end as amount
from A;
Related
This question already has answers here:
Not equal <> != operator on NULL
(10 answers)
Closed 4 years ago.
I'm trying to write a query, that selects a number of values. I only want it to select one of the values if it isn't null.
I'm trying to use a case when but it is erroring.
SELECT pick_no,
pd.product,
from_warehouse,
to_warehouse,
qty_pick,
qty_check,
qty_picked,
qty_checked,
long_description,
ROUND(qty_pick / stk.pallet_unit_qty, 2) as [PalletQty],
ph.date_picking,
stk.bin_no,
CASE WHEN qty_picked <> null
THEN ROUND(qty_picked / stk.pallet_unit_qty, 2) as [pltCheck]
ELSE '0' END
FROM
Null is a tricky beast, you can't use equality operands on it. Also your else clause was returning a CHAR when the first clause returns a FLOAT :
CASE WHEN qty_picked IS NOT NULL
THEN ROUND(qty_picked / stk.pallet_unit_qty, 2) as [pltCheck]
ELSE 0 END
A simpler way to achieve your goal is to use COALESCE :
COALESCE(ROUND(qty_picked / stk.pallet_unit_qty, 2),0) as [pltCheck]
I am having difficulty when trying to have 3 conditions to fulfill in CASE WHEN in SQL server. Here is my original script but only return value 0 instead of 1 and 0. My purpose is to check when a customer purchase ITEM_01 with more than or equal to 0.06 AND ITEM_02 with more than or equal to 0.06 THEN return value 1 else 0. Here is my script:
CASE WHEN SUM ( CASE WHEN ITEM_01)='DH' AND (ITEM_02)='DH Classic' THEN NVL
(SALE_OUTLETD.PRE_SALES_QTY * SKU_UOM_CONV.MULTIPLIER,0) + NVL
(SALE_OUTLETD.SALES_QTY * SKU_UOM_CONV.MULTIPLIER, 0) -NVL
(SALE_OUTLETD.FRESH_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) - NVL
(SALE_OUTLETD.OLD_RTN_QTY * SKU_UOM_CONV.MULTIPLIER,0) - NVL
(SALE_OUTLETD.DAMAGED_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) - NVL
(SALE_OUTLETD.WITHDRAWAL_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) ELSE 0 END )
>= 0.06 THEN 1 ELSE 0 END 3_PACK_AND_ABOVE
Please help on this question because I have been stuck for few hours now. Any help would be greatly appreciated!
I am finding your example impossible to read but the basic form of CASE is:
select case WHEN first_boolean THEN first_value
WHEN second_boolean THEN second_value
WHEN third_boolean THEN third_value
...
ELSE default_value
Always try to build things off that model. Nested CASE statements like what you are doing are never fun and tend to be fairly error prone.
This question already has answers here:
Using IF ELSE in Oracle
(2 answers)
Closed 10 years ago.
I am wondering if there if possibility to achieve some thing like
'if-elseif-else' condition , i know there is a 'case-when-then-else' but it checks only one condition at a time (if i understand it correctly). How can i achieve if-elseif-else scenario in Oracle sql
You can use if/else using case statements like this.
SELECT ename, CASE WHEN sal = 1000 THEN 'Minimum wage'
WHEN sal > 1000 THEN 'Over paid'
ELSE 'Under paid'
END AS "Salary Status"
FROM emp;
i know there is a 'case-when-then-else' but it checks only one
condition at a time
What you are describing is a SIMPLE case. Oracle has two case types: SIMPLE and SEARCHED (see here for more info http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm)
SIMPLE
case A
when 1 then 'foo'
when 2 then 'bar'
else ..
end
SEARCHED
case
when A=1 and B='A' then 'foo'
when D + C =1 and B !='A' then 'Bar'
else ..
end
you probably want to use a searched case. You can use them in PL/SQL or SQL. eg in SQL
select ..
from table
where case
when A=1 and B='A' then 'foo'
when D + C =1 and B !='A' then 'Bar'
else ..
end = 'foo'
Look out for "Decode in Oracle"
decode( expression , search , result [, search , result]... [, default] )
It is similar to "if-elseif-else"
Refer: http://www.techonthenet.com/oracle/functions/decode.php
NOTE: It does only equality checks..
I have seen similar questions asked elsewhere on this site, but more in the context of optimization.
I am having an issue with the order of execution of the conditions in a WHERE clause. I have a field which stores codes, most of which are numeric but some of which contain non-numeric characters. I need to do some operations on the numeric codes which will cause errors if attempted on non-numeric strings. I am trying to do something like
WHERE isnumeric(code) = 1
AND CAST(code AS integer) % 2 = 1
Is there any way to make sure that the isnumeric() executes first? If it doesn't, I get an error...
Thanks in advance!
The only place order of evaluation is guaranteed is CASE
WHERE
CASE WHEN isnumeric(code) = 1
THEN CAST(code AS integer) % 2
END = 1
Also just because it passes the isnumeric test doesn't guarantee that it will successfully cast to an integer.
SELECT ISNUMERIC('$') /*Returns 1*/
SELECT CAST('$' AS INTEGER) /*Fails*/
Depending upon your needs you may find these alternatives preferable.
Why not simply do it using LIKE?:
Where Code Not Like '%[^0-9]%'
Btw, either using my solution or using IsNumeric, there are some edge cases which might lead one to using a UDF such as 1,234,567 where IsNumeric will return 1 but Cast will throw an exception.
Why not use a CASE statement to say something like:
WHERE
CASE WHEN isnumeric(code) = 1
THEN CAST(code AS int) % 2 = 1
ELSE /* What ever else if not numeric */ END
You could do it in a case statement in the select clause, then limit by the value in an outer select
select * from (
select
case when isNum = 1 then CAST(code AS integer) % 2 else 0 end as castVal
from (
select
Case when isnumeric(code) = 1 then 1 else 0 end as isNum
from table) t
) t2
where castval = 1
I have some negative values coming back from a query. I would like them to just be zero.
How do I write a condition in my sql query that returns zero if the value is below a certain value.
sol:
CASE WHEN CONVERT(float,dt.FQI53X02_101) < 1.3 THEN 0 ELSE CONVERT(float,dt.FQI53X02_101) END AS FQI53X02_101
You dont use If-Then-Else in the actual query (you can use them but thats something else)...
What you use is a Case statement... Try this
Select
Case When [Value] < 0 Then 0 Else [Value] End
From
Example
If you want it as part of your query, wrap the return inside a CASE statement. Example from MSDN is below
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
( ABS(Value) + Value ) / 2
edit - this doesn't work now the question has changed