IF statement within a SELECT - SQL [duplicate] - sql

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]

Related

How to add a leading 0 in sql-server [duplicate]

This question already has answers here:
Pad a string with leading zeros so it's 3 characters long in SQL Server 2008
(18 answers)
Closed 10 months ago.
I have to add a leading 0 in a column called houses, where it is a unique list of 3 digits, but I wanted have to add a leading 0 to it --> so 4 digits instead of 3.
Can someone help me with this query as the code is incompatible in the '|' operator.
The code is as follows:
select houses from house_numbers
order by houses;
select houses,
case
when len(houses)=3 then '0' | houses
when len(houses)=4 then '' | houses
end as houses
from house_numbers
The string concatenation operator in SQL Server is +, not ||, so you should use:
CASE
WHEN LEN(houses) = 3 THEN '0' + houses
WHEN LEN(houses) = 4 THEN '' + houses
END AS houses
However, a better way to do this would be to just left pad with zero to a length of 4:
RIGHT('0000' + ISNULL(houses, ''), 4) AS houses
You are looking for the CONCAT function here, assuming you are dealing with varchars
when len(houses)=3 then CONCAT('0' , houses)
You could simplify like so
select houses from house_numbers
order by houses;
select houses,
case
when len(houses)=3 then CONCAT('0' , houses)
else houses
end as houses
from house_numbers
Always add the Zero and then strip of the right 4 chars, I believe this might be less resource intensive than doing a case
SELECT RIGHT('0'+CONVERT(VARCHAR(10),345),4) AS Three_Digit_Example,
RIGHT('0'+CONVERT(VARCHAR(10),1345),4) AS Four_Digit_Example

Decode to Case Statement [duplicate]

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;

Is null > 0 (integer) [duplicate]

This question already has answers here:
What is NULL in SQL?
(5 answers)
Closed 8 years ago.
If I have in my sql something like that:
SELECT *
FROM SOMETABLE
WHERE ((SELECT ONECOLUMN FROM ANOTHERTABLE WHERE ID = 42) > 0)
If I got a NULL in ONECOLUMN it will be greater then 0?
As NULL means "not known", NULL is not greater than 0. It's not smaller either. It's not known. Hence NULL > 0 results in NULL, rather than in TRUE or FALSE.
NULL is not greater than zero.
NULL is not equal to zero.
NULL is not less than zero.
Any given integer you might choose would meet exactly one of these three conditions. But not NULL.
NULL isn't an integer. It's a marker indicating that a value is not present. This could mean that a value exists, but is unknown. It could also indicate that no value exists in this context.
You can use the ISNULL function to find out whether a NULL is present instead of a value. But if you compare a value with zero, and there is a NULL in place of the value, you won't get either TRUE or FALSE as a result.
If you are confused, you're in good company.
How about checking for NULL and then returning 0 if true otherwise return the value of ONECOLUMN:
SELECT *
FROM SOMETABLE
WHERE (
SELECT CASE WHEN ISNULL(ONECOLUMN) THEN 0 ELSE ONECOLUMN END
FROM ANOTHERTABLE
WHERE ID = 42
) > 0
Null is unknown and it cannot be compared to any value. It cannot be greater or lesser.
SELECT *
FROM SOMETABLE
WHERE (
IsNull((SELECT ONECOLUMN
FROM ANOTHERTABLE
WHERE ID = 42), IntValue) > 0
)
If you need to accept the Null so IntValue can be 1 otherwise -1 is OK.

Prepend 0s to string (to specific length) [duplicate]

This question already has answers here:
Formatting Numbers by padding with leading zeros in SQL Server
(14 answers)
Closed 9 years ago.
I have a query where I'm pulling in a check number from the database. The return value needs to always be 9 characters exactly. Most of the check numbers are only 6 characters, so I want to prepend 0s to to the string to ensure it is always exactly 9 characters. Since the check numbers can vary in length, it will be a different number of 0s for each one. What's the most efficient way to do this?
If they're always between 6 and 9 digits, the easiest (and probably best) way to go is using CASE.
SELECT CASE WHEN LEN(CHECK_NUM)= 6 THEN '000' WHEN LEN(CHECK_NUM) = 7 THEN '00'
WHEN
LEN(CHECK_NUM) = 8 THEN '0' ELSE '' END + CHECK_NUM
FROM TABLE
EDIT
In case the original values are numbers (int), try something like this:
SELECT CASE WHEN LEN(CHECK_NUM)= 6 THEN '000' WHEN LEN(CHECK_NUM) = 7 THEN '00'
WHEN
LEN(CHECK_NUM) = 8 THEN '0' ELSE '' END + CAST(CHECK_NUM AS VARCHAR(9))
FROM TABLE1
Take a look at the this SQL Fiddle.

if-elseif-else 'condition' in oracle SQL [duplicate]

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..