IF, ELIF, ELSE in T-SQL - sql-server-2008

I am working in SQL Server 2008 and trying to use a IF, ELIF, ELSE statement in the SELECT section of my code. What I want to do is the following:
IF BO.VALUE < BO.REFERENCELOWERLIMIT
THEN (BO.VALUE - BO.REFERENCELOWERLIMIT) #I WANT THIS TO BE NEGATIVE
ELSE IF BO.REFERENCELOWERLIMIT <= BO.VALUE <= BO.REFERENCEUPPERLIMIT
THEN BO.VALUE
ELSE
(BO.REFERENCEUPPERLIMIT - BO.VALUE)
The problem is that I do not understand how to do a IF, ELIF, ELSE type transaction in SQL. I have tried to search for this type of example and came across python examples...wrong language so I did a search on the MSDBN site and did not see this sort of work, just IF/ELSE.
Thank You

You want a CASE expression. CASE evaluates in order and the first match is what is returned in the query.
SELECT
CASE WHEN BO.VALUE < BO.REFERENCELOWERLIMIT
THEN (BO.VALUE - BO.REFERENCELOWERLIMIT)
WHEN BO.VALUE BETWEEN BO.REFERENCELOWERLIMIT AND BO.REFERENCEUPPERLIMIT
THEN BO.VALUE
ELSE (BO.REFERENCEUPPERLIMIT - BO.VALUE)
END as MyColumnAlias
...

SELECT
col = CASE
WHEN BO.VALUE < BO.REFERENCELOWERLIMIT
THEN BO.VALUE - BO.REFERENCELOWERLIMIT
WHEN BO.VALUE BETWEEN BO.REFERENCELOWERLIMIT AND BO.REFERENCEUPPERLIMIT
THEN BO.VALUE
ELSE BO.REFERENCEUPPERLIMIT - BO.VALUE
FROM tbl

Related

Combining multiple condition in single case statement

Does Invantive SQL support multiple condition in a single case statement? I the statement below, I did not get any results. Tried the same statement with only 1 condition (no cascade), this retrieved the expected result.
select prj.code
, prj.startdate
, prj.enddate
from exactonlinerest..projects prj
where prj.code between $P{P_PROJECT_FROM} and $P{P_PROJECT_TO}
and case
/* when (prj.enddate is null or prj.enddate >= sysdate)
then 'Y'
when (prj.enddate is not null and prj.enddate <= sysdate)
then 'N' */
when prj.startdate <= sysdate
then 'B'
end
= $P{P_PROJECT_ACTIVE_FROM}
I think you where clause is not correctly formulated. With Exact Online, a project either has:
option 1: no end date,
option 2: an end date in the past
option 3: or an end date in the future
The first part of the case handles option 1 and option 3. The second part handles option 2. So there is never an outcome of 'B' in the case.
To analyze such problems, I recommend include the case in the select clause and removing the filter. That gives you a view of the possible outcomes.
Example:
use 868056,102673
select prj.division
, prj.code
, prj.startdate
, prj.enddate
, case
when prj.enddate is null or prj.enddate >= sysdate
then 'Y'
when prj.enddate is not null and prj.enddate <= sysdate
then 'N'
when prj.startdate <= sysdate
then 'B'
end
indicator
from exactonlinerest..projects prj
where prj.code between $P{P_PROJECT_FROM} and $P{P_PROJECT_TO}

combining two case queries into one

I want to make a query where where i first check the current date with the date in a column and then base on that I am writing my case. this query is working fine individually but when
i am combining them its not working.
The queires are
SELECT MONTH(CURRENT_DATE)= SUBSTRING(yearmonth,6) FROM dp;
SELECT i,
CASE
WHEN DAY(CURRENT_DATE) =1 THEN `d1_v`
WHEN DAY(CURRENT_DATE) =2 THEN `d1_v`
END VALUE
FROM dp;
combined query..
SELECT i,
CASE
WHEN((MONTH(CURRENT_DATE ))= SUBSTRING(yearmonth,6) THEN
(CASE
WHEN DAY(CURRENT_DATE) = 1 THEN `d1_v`
WHEN DAY(CURRENT_DATE) = 2 THEN `d1_v`
END VALUE)END)Y
FROM dp
please guide me
You've to delete the '(' after the THEN:
SELECT i,
CASE
WHEN ((MONTH(CURRENT_DATE )) = SUBSTRING(yearmonth,6))THEN
CASE
WHEN DAY(CURRENT_DATE) = 1 THEN `day1_value`
WHEN DAY(CURRENT_DATE) = 2 THEN `day1_value`
END
END Y
FROM dp;
The output from the sqlfiddle is 5,null right now.
Hope this work for you.
The sqlfiddle is: http://sqlfiddle.com/#!2/e59c5/10

Conditional where in SQL based on Value?

I am doing a report whereby I can't construct the SQL programatically. I have two values that are fed into the report. The values that can be one of the following three options:
redeem
purchase
redeem AND purchase
The query needs to have a WHERE clause. If "redeem" is fed in, it must have:
... WHERE balance < 0
If "purchase" is fed in, it must have:
... WHERE balance >= 0
if both are fed in, this condition can be left out completely, or it can be said:
... WHERE balance >= 0 OR balance < 0 --> but this is redundant
Is there a way to apply this kind of logic in SQL? Is something like this possible in SQL:
SELECT * FROM account WHERE (if param1 = 'redeem' then 'balance <= 0) ... etc
?
Yep. You're almost there.
WHERE (param='redeem' and balance <=0) or (param='purchase' and balance>=0) or (param='redeem AND purchase ')
Use CASE statements in your WHERE clause. You can find examples here.
I tried the following code in MySQL and it works:
SET #param = 'purchase';
SELECT * FROM TEST.ACCOUNT
WHERE CASE
WHEN #param = 'redeem' THEN BALANCE < 0
WHEN #param = 'purchase' THEN BALANCE >= 0
ELSE TRUE
END;
Would this do the trick?
WHERE
(purchase is not null && balance >= 0)
OR
(redeem is not null && balance < 0)
CASE WHEN param1 = 'redeem' THEN balance <= 0

MySQL IF ELSEIF in select query

I'm trying to select different prices of a product based on the quantity that user chooses.
This is the query I'm working on (it has a syntax error):
select id,
(SELECT
IF(qty_1<='23',price,1)
ELSEIF(('23'>qty_1 && qty_2<='23'),price_2,1)
ELSEIF(('23'>qty_2 && qty_3<='23'),price_3,1)
ELSEIF('23'>qty_3,price_4,1)
END IF) as total
from product;
You have what you have used in stored procedures like this for reference, but they are not intended to be used as you have now. You can use IF as shown by duskwuff. But a Case statement is better for eyes. Like this:
select id,
(
CASE
WHEN qty_1 <= '23' THEN price
WHEN '23' > qty_1 && qty_2 <= '23' THEN price_2
WHEN '23' > qty_2 && qty_3 <= '23' THEN price_3
WHEN '23' > qty_3 THEN price_4
ELSE 1
END) AS total
from product;
This looks cleaner. I suppose you do not require the inner SELECT anyway..
IF() in MySQL is a ternary function, not a control structure -- if the condition in the first argument is true, it returns the second argument; otherwise, it returns the third argument. There is no corresponding ELSEIF() function or END IF keyword.
The closest equivalent to what you've got would be something like:
IF(qty_1<='23', price,
IF('23'>qty_1 && qty_2<='23', price_2,
IF('23'>qty_2 && qty_3<='23', price_3,
IF('23'>qty_3, price_4, 1)
)
)
)
The conditions don't all make sense to me (it looks as though some of them may be inadvertently reversed?), but without knowing what exactly you're trying to accomplish, it's hard for me to fix that.
I found a bug in MySQL 5.1.72 when using the nested if() functions .... the value of column variables (e.g. qty_1) is blank inside the second if(), rendering it useless. Use the following construct instead:
case
when qty_1<='23' then price
when '23'>qty_1 && qty_2<='23' then price_2
when '23'>qty_2 && qty_3<='23' then price_3
when '23'>qty_3 then price_4
else 1
end
For your question :
SELECT id,
IF(qty_1 <= '23', price,
IF(('23' > qty_1 && qty_2 <= '23'), price_2,
IF(('23' > qty_2 && qty_3 <= '23'), price_3,
IF(('23' > qty_2 && qty_3<='23'), price_3,
IF('23' > qty_3, price_4, 1))))) as total
FROM product;
You can use the if - else control structure or the IF function in MySQL.
Reference:
http://easysolutionweb.com/sql-pl-sql/how-to-use-if-and-else-in-mysql/
As per Nawfal's answer, IF statements need to be in a procedure. I found this post that shows a brilliant example of using your script in a procedure while still developing and testing. Basically, you create, call then drop the procedure:
https://gist.github.com/jeremyjarrell/6083251

Mysql: Set variable using a case from a variable

What I have is a bit column NonMileage and based on that bit column i want to make a variable that I can use inside of a where clause.
this is a two part question:
How do you case a variable? The code below does not case the
#NoMileageListing
And then I have it setting #MileListingClause as a string, can I
just use #MileListingClause like where #MileListingClause?
.
SET #NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT
#NoMileageListing CASE #NoMileageListing when 1 then
SET #MileListingClause = 'tblcargarage.miles >= 0' else
SET #MileListingClause = 'tblcargarage.miles != 0' end case;
here's the answer
SET #NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT CASE #NoMileageListing
WHEN 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO #NoMileWhereClause;
select #NoMileWhereClause;
found here:
Mysql Storing a variable with the result of an SELECT CASE
I think the problem is located in the when part.
It should be:
SELECT CASE #NoMileageListing
WHEN #NoMileageListing = 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO #NoMileWhereClause;
select #NoMileWhereClause;
to create the dynamic query with the #noMileWhereClause refer to the answer here:
How To have Dynamic SQL in MySQL Stored Procedure