how to use > = condition in sql case statement? - sql

I am trying to place a >= condition in statement, but it is not allowing this condition.
case
when ct.CRS_CAREER_LVL_CD = 'G' then 'Graduate '
when ct.CRS_CAREER_LVL_CD = 'L' then 'Law '
when convert(int, left(ct.CRS_CATLG_NO, 4) > = 99 then 'Upper Division'
else 'Lower Division'
end as courseLevelName
Is there any other way to do this ?

when convert(int,left(ct.CRS_CATLG_NO,4)
^ ^ ^ ^
OPEN OPEN CLOSE CLOSE ?

Guessing the problem is that the left 4 characters of are not always integers, if you're using SQL Server 2012 or newer you can use TRY_CONVERT():
case when ct.CRS_CAREER_LVL_CD = 'G' then 'Graduate '
when ct.CRS_CAREER_LVL_CD = 'L' then 'Law '
when TRY_CONVERT(int,left(ct.CRS_CATLG_NO,4)) > = 99 then 'Upper Division'
else 'Lower Division'
end as courseLevelName
Edit: Looks like you were missing a closing ), if that wasn't a typo then that's likely the issue, if still getting an error then might be non-INT.

Related

If statement in SQL how to do it

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.

SQL Query Error with CASE Statement?

I'm attempting to run this query using Simba's ODBC SFDC driver but the log shows me an error near the case statement. I'm not totally convinced its an error with the CASE statement but I don't see where my error is. Someone please help!!!!
SELECT
Account_Group__c,
Hospital_Sales_Teammate__c,
Name,
StageName,
CloseDate,
Yr_Credited__c,
Probability,
Census__c,
Credit__c,
Related_VSA__c,
AB_Hospital_Relationship_Type__c,
CASE
WHEN Age_In_Stage__c >0 and Age_In_Stage__c <= 30 THEN '<30'
WHEN Age_In_Stage__c >30 and Age_In_Stage__c <= 60 THEN '31-60'
WHEN Age_In_Stage__c >60 and Age_In_Stage__c <= 90 THEN '61-90'
ELSE '>90' END AS Age_Bucket,
CASE
WHEN (Type = "Existing Business - Renewal" OR Type = 'Existing Business - Amendment')
AND (Account_HHV_Segment__c='A' OR Account_HHV_Segment__c='B')
AND AB_Hospital_Relationship_Type__c<>'N/A'
AND (RecordType='012300000000PWuAAM'
OR RecordType='01250000000DcJkAAK'
OR RecordType='01250000000DpV4AAK'
OR RecordType='01250000000Dxd7AAC'
OR RecordType='01250000000DoFPAA0'
OR RecordType='01250000000DuuEAAS') THEN 'Hosp'
WHEN Name LIKE '%AB Hospital Loss%' THEN 'Hosp'
ELSE '' END AS Hospital_Eligible,
CASE
WHEN RecordType='01250000000DpV4AAK'
AND Type LIKE '%Acquisition%'
THEN 'Acq'
ELSE '' END AS Acquisition_Eligible,
CASE
WHEN RecordType='01250000000Dxd7AAC'
AND (Business_Unit__c="Full Conversion" OR Business_Unit__c="Partial Conversion")
THEN 'BGC'
ELSE '' END AS Conversion_Eligible,
CASE
WHEN RecordType='01250000000DuuEAAS'
AND Type_of_Agreement__c ="MDA" OR Type_of_Agreement__c ="Joinder" OR Type_of_Agreement__c ="JV"
THEN 'Incr Doc'
ELSE '' END AS Incr_Doc_Eligible
FROM
Opportunity
WHERE
Eligible__c<>'No'
AND NOT Name LIKE '%test%'
AND NOT Name LIKE '%Test%'
AND NOT Name LIKE '%TEST%'
ORDER BY
Account_Group__c ASC
Business_Unit__c="Full Conversion" (and other places as well): You are using double quotes instead of single quotes (as you do in the rest of the query). I bet that's the problem...
Also, this is a case expression, not a statement.
Why are you using double quotes?
(Type = "Existing Business - Renewal" OR Type = 'Existing Business - Amendment')
You should change it to
(Type = 'Existing Business - Renewal' OR Type = 'Existing Business - Amendment')

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

SQL simplifying case in case in case

I got the following SQL code (part of a select Statement):
Case
When HilfsTab2.Gruppe = 'HST' And Basis.Breite_FLA = Basis.Breite Then 0
Else Case When HilfsTab2.Gruppe = 'SA' Or HilfsTab2.Gruppe = 'HO / TB' Or
HilfsTab2.Gruppe = 'PR' Then 0 Else Case
When HilfsTab2.Gruppe Is Null Then -1 Else 1 End End
End As IsHST_Fluegel
Now, I run this over a table of several million entries. From my understanding, SQL checks the first case when for all rows, then the second for all entries and so on. This takes ages. Now I was thinking, there needs to be an easier way to do this.
I was thinking of a stored procedure / custom function that basically outputs -1, 0 or 1 depending on the entry.
Thanks in advance
For a possible speed improvement, do the NULL check first, the column comparison last and refactor to remove the nested CASE:
CASE WHEN HilfsTab2.Gruppe IS NULL
THEN -1
WHEN HilfsTab2.Gruppe IN ('SA', 'HO / TB', 'PR')
OR (HilfsTab2.Gruppe = 'HST' AND Basis.Breite_FLA = Basis.Breite)
THEN 0
ELSE 1
END AS IsHST_Fluegel
Your case could be simplified as:
Case
When HilfsTab2.Gruppe = 'HST' And Basis.Breite_FLA = Basis.Breite Then 0
When HilfsTab2.Gruppe in ('SA', 'HO / TB', 'PR') Then 0
When HilfsTab2.Gruppe Is Null Then -1
Else 1
End As IsHST_Fluegel
But this will not speed up your query. If you want to select millions of rows, it would take time anyway.

Conditional WHERE clause with CASE statement in Oracle

I'm brand-new to the Oracle world so this could be a softball. In working with an SSRS report, I'm passing in a string of states to a view. The twist is that the users could also pick a selection from the state list called "[ No Selection ]" ... (that part was not by doing and I'm stuck with implementing things this way)
If they choose the No Selection option, then I just want to return all states by default, otherwise return just the list of states that are in my comma-separated list.
This really seems like it should be easy but I'm stuck. Here is the code I have so far (just trying to get a sample to work) but my eyes have finally gone crossed trying to get it going.
Could anybody give me some direction on this one please?
begin
:stateCode:='MO,FL,TX';
--:stateCode:='[ No Selection ]';
end;
/
select count(*) as StateCount, :stateCode as SelectedVal
from hcp_state vw
where
(case
when (:stateCode = '') then (1)
when (:stateCode != '') then (vw.state_cd in (:stateCode))
(else 0)
end)
;
You can write the where clause as:
where (case when (:stateCode = '') then (1)
when (:stateCode != '') and (vw.state_cd in (:stateCode)) then 1
else 0)
end = 1;
Alternatively, remove the case entirely:
where (:stateCode = '') or
((:stateCode != '') and vw.state_cd in (:stateCode));
Or, even better:
where (:stateCode = '') or vw.state_cd in (:stateCode)
We can use a CASE statement in WHERE clause as:
SELECT employee_no, name, department_no
FROM emps
WHERE (CASE
WHEN :p_dept_no = 50 THEN 0
WHEN :p_dept_no = 70 THEN 0
ELSE -1
END) = 0;