If statement in SQL how to do it - sql

I've got an if statement (below) which runs in Tableau. I need to turn this into SQl - I've tried lots of different case statements, but they all get errors.
Can anyone advise how I'd do this?
IF LEN(REPLACE([postcode]," ","")) = 7 THEN LEFT(REPLACE([postcode]," ",""),4)+" "+RIGHT(REPLACE([postcode]," ",""),3)
ELSEIF LEN(REPLACE([postcode]," ","")) = 6 THEN LEFT(REPLACE([postcode]," ",""),3)+" "+RIGHT(REPLACE([postcode]," ",""),3)
ELSEIF LEN(REPLACE([postcode]," ","")) = 5 THEN LEFT(REPLACE([postcode]," ",""),2)+" "+RIGHT(REPLACE([postcode]," ",""),3)
END

Try to use this:
SELECT
CASE
WHEN LEN(REPLACE([postcode],' ','')) = 7
THEN LEFT(REPLACE([postcode],' ',''),4)+' '+RIGHT(REPLACE([postcode],' ',''),3)
WHEN LEN(REPLACE([postcode],' ','')) = 6
THEN LEFT(REPLACE([postcode],' ',''),3)+' '+RIGHT(REPLACE([postcode],' ',''),3)
WHEN LEN(REPLACE([postcode],' ','')) = 5
THEN LEFT(REPLACE([postcode],' ',''),2)+' '+RIGHT(REPLACE([postcode],' ',''),3)
ELSE 'ERROR'
END
FROM YOUR_TABLE

As the OP stated after some querying, he is using Hive on Hadoop - not plain SQL.
So - maybe - this could get you started: https://de.hortonworks.com/blog/hive-cheat-sheet-for-sql-users/

You could just replace the IF in your statement by CASE WHEN
And all the ELSEIF by WHEN
But you can probably also simplify it to :
CASE
WHEN LEN(REPLACE([POSTCODE]," ","")) IN (5,6,7) THEN REPLACE ([POSTCODE]," ","")
ELSE NULL
END
Since f.e. "ABC" + "DEF" = "ABCDEF" = "ABC DEF" without spaces.

Related

Converting Access SQL to T-SQL

I have a created field in an Access database that I am trying to re-create in T-SQL. I am getting some incorrect results and I think it is how I wrote the code. Here is the field in Access that is working correctly:
MCP Actual: IIf([lever]="MCP",[actual usd]*IIf([split flag]="x",[split percent],1))*[Allocation Value]
Here is how I have it coded in SQL:
MCPActual =
CASE
WHEN pbd.Lever = 'MCP' THEN pbd.ActualUSD * CASE WHEN ou.SplitFlag = 'x' THEN ((pbd.ActualUSD * ou.SplitPercent) * pda.AllocationValue) END
ELSE ((pbd.ActualUSD * 1) * pda.AllocationValue)
END
I think you haven't got the END and brackets in the correct sequence try
CASE
WHEN pbd.Lever = 'MCP' THEN pbd.ActualUSD *
(CASE WHEN ou.SplitFlag = 'x' THEN
(pbd.ActualUSD * ou.SplitPercent * pda.AllocationValue )
ELSE
(pbd.ActualUSD * pda.AllocationValue) END)
END
You don't need to multiply by 1 unless you are trying to covert a negative / positive value.

CASE with IN clause

I have a condition (in Business objects ) as below
=If([Actual ]=0;Sum([Applied] In ([Project];[ Name];[Number];[Sub ])))
which I need to convert it to CASE statement for my OBIEE
Below is the query I had tried but it doesn't work:
SELECT
CASE
WHEN F.ACTUAL_EQP_COST = 0
THEN SUM((F.HRS_APPLIED) IN(F.PROJECT,F.NAME,F.NUMBER,F.SUB))
ELSE 0
END
FROM F
Probably
select SUM(F.HRS_APPLIED)
from DTS_OSC_WIP_REP_CST_ANALYSIS_A F
where F.ACTUAL_EQP_COST = 0
group by F.PROJECT,F.WIP_ENTITY_NAME,F.OPERATION_NUMBER,F.OPERATION_SUB_SEQ
You can't use "in" inside a "then" statement.
I couldn't understand exactly but something in this direction might help:
select
CASE WHEN F.ACTUAL_EQP_COST = 0
THEN (SELECT SUM(F1.HRS_APPLIED)
FROM DTS_OSC_WIP_REP_CST_ANALYSIS_A F1
WHERE F1.column_name IN (F.PROJECT,F.WIP_ENTITY_NAME,F.OPERATION_NUMBER,F.OPERATION_SUB_SEQ))
ELSE 0 END
from DTS_OSC_WIP_REP_CST_ANALYSIS_A F

case statement in where clause SQLSERVER

I tried searching around but couldn't find anything that would help me out.
I'm trying to do this in SQL:
DECLARE #onlyMM INT
SET #onlyMM = 1
SELECT *
FROM cdn.magnag
WHERE CASE #onlyMM
WHEN 1
THEN man_zrdtyp NOT IN (
1616
,2001
)
ELSE - 1
END
I have a problem with:
where case #onlyMM when 1 then man_zrdtyp not in (1616,2001) else -1 end
how to properly make a case for the operator not in?
I think you need to formulate the WHERE clause in a different way. You could use OR instead of case statement. Like this:
WHERE
(#onlyMM=1 AND man_zrdtyp not in (1616,2001))
OR #onlyMM<>1
You don't want to do this?
where (case when #onlyMM = 1 then man_zrdtyp else -1 end) not in (1616,2001)

RODBC "invalid character\n" error when using CASE WHEN statement

I'm attempting to execute a simple "if/then" statement on an Oracle SQL database using RODBC in R. The SQL statement works fine in SQL Developer v4.0.2.15 but throws an error when performing the same statement in R
sqlQuery(channel, "
select
Variable1,
Variable2,
CASE WHEN Variable1 = 0 then 0 else 1 end as Var3
from schema.TABLE
where ROWNUM<100;
"
)
The error message (updated):
[1] "[1] "HY000 936 [Oracle][ODBC][Ora]ORA-00936: missing expression\n"
[2] "[2] ...
The statement works fine when removing the CASE WHEN line, so the error must be in the CASE WHEN syntax.
I've found a semicolon sometimes causes problems in cases like this.
> sqlQuery(con, "select case when dummy = 'X' then 1 else 0 end from dual")
CASEWHENDUMMY='X'THEN1ELSE0END
1 1
> sqlQuery(con, "select case when dummy = 'X' then 1 else 0 end from dual;")
[1] "HY000 911 [Oracle][ODBC][Ora]ORA-00911: invalid character\n"
[2] "[RODBC] ERROR: Could not SQLExecDirect 'select case when dummy = 'X' then 1 else 0 end from dual;'"
> close(con)
If you look at the SQL statement (which seems to appear in it's entirety in the error message), we see:
'\nselect\n UW_NET_ULTIMATE_LOSS_USD,\n UW_NET_WRITTEN_PREMIUM_USD,\n
IF UW_NET_WRITTEN_PREMIUM_USD = 0 THEN testvar=0 ELSE testvar=1 end\n
from bqiread.POLICY_METRICS\nwhere ROWNUM<100;\n
There's no CASE statement here....it apprears to be an IF/THEN/ELSE, which is not valid Oracle syntax.
Perhaps your tool is generating SQL for a different RDBMS than Oracle, and attempting to execute it in Oracle?

How to use case when in SQL Server 2008

I want to use case after where clause in SQL Server 2008; how can I use it like
select *
from MPR
where
case when #min >= 0
then MainComponent_Id = #mainc and SubComponent_Id = #subcomp
else MainComponent_Id = #mainc and SubComponent_Id = #subcomp and MinorComponent_Id = #minorc
end
end
I am using a stored procedure, and in my stored procedure if minorcomponent_id value is 0 then it will add 1 one else 2 one will work.
I had tried hard but its giving error in near case.
How to resolve it?
Thanks
You don't need to use a CASE statement. You can simplify logic as follows:
select
*
from
MPR
where
MainComponent_Id = #mainc AND SubComponent_Id = #subcomp
AND (#min >= 0 OR MinorComponent_Id = #minorc)