I have the following:
set #SomeVariable = #AnotherVariable/isnull(#VariableEqualToZero,1) - 1
If #VariableEqualToZero is null it substitutes the 1. I need it to substitute 1 if #VariableEqualToZero = 0 as well. How do I do this?
If you're using SQL Server, you can probably use a NULLIF statement?
i.e. set the value to NULL if it's 0 then set it to 1 if it's NULL - should catch for both 0's and NULLs:
SET #SomeVariable = #AnotherVariable/ISNULL(NULLIF(#VariableEqualToZero,0),1) - 1
SET #SomeVariable = #AnotherVariable / COALESCE(
CASE
WHEN #VariableEqualToZero = 0 THEN 1
ELSE #VariableEqualToZero
END, 1) - 1
set #SomeVariable = #AnotherVariable /
(case when isnull(#VariableEqualToZero, 0) = 0 then 1 else
#VariableEqualToZero end) - 1
You use CASE
instead of
ISNULL(#VariableEqualToZero,1)
use
CASE WHEN #VariableEqualToZero IS NULL OR #VariableEqualToZero = 0 THEN 1 ELSE #VariableEqualToZero END
COALESCE and ISNULL are essentially just shortcuts for a CASE statement.
You can consult the help for the syntax of CASE.
Related
I am using SQL, IBM Data Studio, db2-400. I need help writing a case statement that looks for the length of a field and returns a blank if it is not equal to 14, I have tried many ways without success, below is my latest attempt.
CASE
WHEN LENGTH(TRIM(FFIUPCN) <> '14'
THEN ''
ELSE
FFIUPCN
END AS "UPC (if applicable)"
The error I get is that the token "<>" is not valid.
Main issue is that you are missing a )
CASE
WHEN LENGTH(TRIM(FFIUPCN)) <> 14
THEN ''
ELSE
FFIUPCN
END AS "UPC (if applicable)"
but you shouldn't be comparing a numeric, returned by length(), to a sring '14'. But Db2 is implicitly converting it.
For multiple conditions you can nest your case statements
Select
case when FIUPCN+FFIUPCN2 = 0
then case when FIUPCN >= value then FIUPCN + 1
else FIUPCN + 2 end
else
case when FIUPCN >= value
then case when FIUPCN <> ''
then FIUPCN + 3
else prvca1 end
else
case
when FIUPCN >= FIUPCN2 and FIUPCN3 <> 0 then FIUPCN + 4
when FIUPCN >= FIUPCNX and FIUPCNY <> 0 then FIUPCN + 5
else prvca1 end
end
end
from table
I'm using a variable to get my SELECT to run in 2 different "modes":
DECLARE #Mode as Varchar(1) = 'A'
SELECT
CASE
WHEN #Mode = 'A'
THEN FieldABC * 2
ELSE FieldABC * 3
END AS FieldABC,
CASE
WHEN #Mode = 'A'
THEN FieldDEF
ELSE FieldGHI
END AS FieldJKL
WHERE
FieldABC BETWEEN 0 AND 100
When I want to run the "B" version of the SELECT, I just have to change the variable value. This all works fine.
I now want to change the WHERE clause so it too is dependent upon the variable:
IF #Mode = 'A' THEN
WHERE FieldABC > 0
ELSE
WHERE FieldABC < 0
END IF
Except, this syntax is totally nonsense in SQL!
How can I achieve the 2 different flavours of my WHERE clause dependent upon the same variable - or some other simple way of switching the SELECT between the 2 different modes?
You can do this with a combination of AND and OR :
WHERE (#Mode='A' AND FieldABC > 0)
OR (#Mode='B' AND FieldABC < 0);
Or with a CASE expression:
WHERE CASE
WHEN #Mode = 'A' AND FieldABC > 0 THEN 1
WHEN #Mode = 'B' AND FieldABC < 0 THEN 1
ELSE 0
END = 1;
I want to get multipule choises after then in case statment as
#value
select * from [dbo].[Currency_Tbl]
WHERE [Currency_Active_YN]=
CASE WHEN #value = 1 THEN
( 1 or 0)
ELSE
#Value = 0 then 0
END
it didn't accept the first line in col1 but accept the col2
how can I select multiple numbers after THEN?
You don't use case in where clauses. Use boolean logic
select * from [dbo].[Currency_Tbl]
WHERE (#value = 1 and [Currency_Active_YN] in (0,1))
OR (#value = 0 and [Currency_Active_YN] = 0)
You dont need a case to do what you're trying to do. Assuming Currency_Active_YN is a not null bit field the following logic should suffice.
select * from [dbo].[Currency_Tbl]
WHERE (#value=1 OR [Currency_Active_YN]=#Value)
How would I achieve the following:
select (1 < 2) as One, (1 > 2) as Two
so that it would yield the following results:
One Two
-----------------
True False
I'm using SQL Server but a cross DBMS example would be good.
Assuming this is SQL server, you can use CASE statement.
select (case when (1 < 2) then 'True' else 'False' end) as one,
(case when (1 > 2) then 'True' else 'False' end) as two
from table
In the place of condition, you can use any variable or any column values too. Basically an expression.
Well, in Oracle you could do something like
SELECT CASE
WHEN 1 < 2 THEN
'TRUE'
ELSE
'FALSE'
END AS ONE,
CASE
WHEN 1 > 2 THEN
'TRUE'
ELSE
'FALSE'
END AS TWO
FROM DUAL;
Note that Oracle doesn't have a BOOLEAN type in the database (as opposed to PL/SQL, which does have BOOLEAN's) so the case expressions return character strings.
Share and enjoy.
Use a case statement:
declare #value1 int, #value2 int
set #value1 = 1
set #value2 = 2
select
case when (#value1 < #value2) then 'True' else 'False' end as One
case when (#value1 > #value2) then 'False' else 'True' end as Two
from table
Depending on your need, you can hard code in the values, or you can do something similar to this for when you may want to change the values. You could also combine the case statement into one column, or break it out for doing less than or equal type comparisons as well.
I have a procedure which receive a bit variable called #FL_FINALIZADA.
If it is null or false I want to restrict my select to show only the rows that contain null DT_FINALIZACAO values. Otherwise I want to show the rows containing not null DT_FINALIZACAO values.
Something like this:
SELECT
*
FROM
MyTable
WHERE
...
AND
(
OPE.DT_FINALIZACAO = (
CASE
WHEN (#FL_FINALIZADA <> 1)
THEN NULL
END
) OR
OPE.DT_FINALIZACAO IS NOT NULL
)
In this case I receive the message:
None of the result expressions in a
CASE specification can be NULL.
How can I achieve this?
Thanks in advance.
SELECT
*
FROM
MyTable
WHERE
(ISNULL(#FL_FINALIZADA, 0) = 0
AND
OPE.DT_FINALIZACAO IS NULL
)
OR
(#FL_FINALIZADA = 1
AND
OPE.DT_FINALIZACAO IS NOT NULL
)
Change the AND to be:
AND (((#FL_FINALIZADA <> 1) AND (OPE.DT_FINALIZACAO IS NULL)) OR ( (#FL_FINALIZADA = 1) AND (OPE.DT_FINALIZACAO IS NOT NULL)))
If the bit flag is 1 then DT_FINALIZACAO can't be null.
IF #FL_FINALIZADA IS NULL
SET #FL_FINALIZADA = 0
SELECT * FROM NewsletterSubscribers
WHERE
(#FL_FINALIZADA = 0 AND OPE.DT_FINALIZACAO IS NULL)
OR
(#FL_FINALIZADA = 1 AND OPE.DT_FINALIZACAO IS NOT NULL)
My detailed SQL is a little rusty, but have you tried using 0 insted of NULL? I would expect 0 to evaluate the same as NULL in that select