Nested Case Statement - Pulling the wrong value - sql

Here is my syntax/question
,CASE WHEN nullif(ltrim(A),'') IS NOT NULL OR nullif(ltrim(B),'') IS NOT NULL THEN NULL
WHEN nullif(ltrim(C),'') IS NOT NULL THEN (
CASE WHEN nullif(MM,'') IS NOT NULL THEN MM
WHEN NN IS NOT NULL THEN XX
WHEN NN IS NULL THEN concat(UPPER(YY), ' ', UPPER(XX))
END
)
WHEN nullif(ltrim(D),'') IS NOT NULL OR nullif(ltrim(E),'') IS NOT NULL THEN concat(UPPER(XX), ' ', UPPER(YY))
ELSE ' '
END as 'Data_Item'
We have a series of statements to evaluate.
If fields A OR B are not null then we pull NULL
if field C is not null then we evaluate a nested case statement and this is where something is off with my code.
if field C is not null we should evaluate field MM if MM is not null we should return MM (this is what i should be getting but i'm not).
we continue to evaluate, if NN is not null we pull XX
if NN is null we concatenate YY space XX
and lastly if field D is not null or field E is not null then we concatenate fields XX space YY.
else we simply return a space
then end
--
In short, its a semi-complex series case statement, if the second scenario is true we have to evaluate like 3 or 4 nested statements. Till we find what is true.
for whatever reason my data is always returning the last statement (D is true) rather than the nested one.

This is your CASE simplified:
CASE
WHEN A <> '' OR B <> ''
THEN NULL
WHEN C <> ''
THEN CASE
WHEN MM <> '' THEN MM
WHEN NN IS NOT NULL THEN XX
WHEN NN IS NULL THEN Concat(Upper(YY), ' ', Upper(XX))
END
WHEN D <> '' OR E <> ''
THEN Concat(Upper(XX), ' ', Upper(YY))
ELSE ' '
END
Seems to match your logic ...

Related

condition where a <> ' ' and a <> '' not working

I wrote a SQL to query table mat from an oracle db where column A is not null. Column A is varchar and its default value is ' '. I wrote the sql below:
select * from mat where matnr='test' and A <>'' and A <> ' '
But it return an empty data set.
Then I ran:
select * from mat where matnr='test' and A <> ' '
This query worked. So what is the reason? Thx.
In Oracle, '' means NULL. Any direct comparison to NULL returns NULL instead of TRUE or FALSE, so you cannot say A <> '' - you must say A IS NOT NULL.
Another possibility would be to use the NVL function, replacing NULL with ' ', so that you could say
select * from mat where matnr='test' and NVL(A, ' ') <> ' '

Concat String columns in hive

I need to concat 3 columns from my table say a,b,c. If the length of the columns is greater than 0 then I have to concat all 3 columns and store it as another column d in the below format.
1:a2:b3:c
I have tried the following query but I am not sure how to proceed as I am getting null as the result.
select a,b,c,
case when length(a) >0 then '1:'+a else '' end + case when length(b) > 0 then '2:'+b else '' end + case when length(c) > 0 then '3:'+c else '' end AS d
from xyz;
Appreciate the help :)
Use concat() function:
select a,b,c,
concat(
case when length(a)>0 then concat('1:',a) else '' end,
case when length(b)>0 then concat('2:',b) else '' end,
case when length(c)>0 then concat('3:',c) else '' end
) as d
from (--test dataset
select stack(4, 'a','b','c', --all
'','b','c', --one empty
null,'b','c', --null
'','','' --all empty
) as (a,b,c)
)your_data;
Result:
OK
a b c 1:a2:b3:c
b c 2:b3:c
NULL b c 2:b3:c
Time taken: 0.284 seconds, Fetched: 4 row(s) - last one row is empty
As of Hive 2.2.0. you can use || operator instead of concat:
select a,b,c,
case when length(a)>0 then '1:'||a else '' end||
case when length(b)>0 then '2:'||b else '' end||
case when length(c)>0 then '3:'||c else '' end as d

Need to print all the missing value column names in SQL Server 2008

I need all the column names missing mandatory values in a single string, in a select statement shown below.
I have tried using CASE statement but it was not working since it will omit the second condition if first one matches.
SELECT
stg2.StageID
,[CSP]
,[CSPRegionID]
,[SoldToCompanyID]
,[EndUserCompanyID]
,[SupportOptionID]
,'One/More Mandatory value/s is/are missing' FailureReason
,CASE WHEN stg2.CSP IS NULL THEN #FailureDetails + 'CSP'
WHEN stg2.CSPRegionID IS NULL THEN #FailureDetails + 'CSP Region'
WHEN stg2.SoldToCompanyID IS NULL THEN #FailureDetails + 'SoldToCompany'
WHEN stg2.EndUserCompanyID IS NULL THEN #FailureDetails + 'EndUserCompany'
WHEN stg2.SupportOptionID IS NULL THEN #FailureDetails + 'SupportOption'
END FailureDetails
FROM [EntitlementTracker_SRV4Stage].[dbo].[srv4_ClassicLoad_Stage2] stg2
WHERE stg2.CSP IS NULL
OR stg2.CSPRegionID IS NULL
OR stg2.SoldToCompanyID IS NULL
OR stg2.EndUserCompanyID IS NULL
OR stg2.SupportOptionID IS NULL
Anybody please help out. Thanks in advance.
You are right, CASE will only evaluate the first case that matches. I'd suggest something like:
, CASE WHEN stg2.CSP IS NULL THEN 'CSP, ' ELSE '' END
+ CASE WHEN stg2.CSPRegionID IS NULL THEN 'CSP Region, ' ELSE '' END
+ CASE WHEN stg2.SoldToCompanyID IS NULL THEN 'SoldToCompany, ' ELSE '' END
+ CASE WHEN stg2.EndUserCompanyID IS NULL THEN 'EndUserCompany, ' ELSE '' END
+ CASE WHEN stg2.SupportOptionID IS NULL THEN 'SupportOption, ' ELSE '' END
FailureDetails

SQL Server : CASE does not work

SELECT
a.componentId, a.uniqueCode,
'sd'= CASE
WHEN RTRIM(LTRIM(b.name)) IS NULL OR RTRIM(LTRIM(b.uniqueCode)) IS NULL
THEN isnull(b.uniqueCode,'')+isnull(b.name,'')
WHEN RTRIM(LTRIM(b.name)) IS NULL AND RTRIM(LTRIM(b.uniqueCode)) IS NULL
THEN isnull(b.uniqueCode,'')+isnull(b.name,'')
ELSE b.uniqueCode + '(' + (b.name) + ')'
END,
a.specialization
FROM Doctors a
LEFT OUTER JOIN Territories b ON a.locationId = b.componentId;
Suppose b.uniqueCode = T003 and b.name = Dhanmondi 01, then sd should be T003(Dhanmondi 01).
Now if b.name = NULL then sd should be T003, but my query result shows T003().
What is wrong my T-SQL query?
Are you sure that b.name is null? If it has an empty string instead of null you would have the result you see. BTW, the rtrim/ltrim stuff is totally unnecessary when checking with is null and your second when will never happen because you will always end up in the first when if either column is null.
This will treat empty strings as null:
SELECT a.componentId, a.uniqueCode, 'sd'=
CASE
WHEN nullif(b.name, '') IS NULL OR nullif(b.uniqueCode, '') IS NULL
THEN isnull(b.uniqueCode,'')+isnull(b.name,'')
ELSE b.uniqueCode + '(' + (b.name) + ')'
END , a.specialization
FROM Doctors a LEFT OUTER JOIN Territories b ON a.locationId = b.componentId;
Let's sort some basics out first...
Your second WHEN is impossible to reach, since the earlier WHEN will always be true (either null) before the second WHEN (both null).
RTRIM() and LTRIM() will only return NULL if the argument is NULL, so these two condition expressions are identical:
RTRIM(LTRIM(b.name)) IS NULL
b.name IS NULL
Removing redundant code, including the unreachable WHEN, will let you simplify your code considerably:
CASE
WHEN b.name IS NULL OR b.uniqueCode IS NULL
THEN isnull(b.uniqueCode,'') + isnull(b.name,'')
ELSE b.uniqueCode + '(' + b.name + ')'
END
Now that we can read it...
The most likely explanation is that b.name is blank, not NULL.
In SQL and all of the popular SQL products - except Oracle - a NULL and an empty string '' are two different things. So, you should be checking both options:
SELECT a.componentId, a.uniqueCode,
CASE
WHEN b.name IS NULL OR b.name = ''
THEN COALESCE(b.uniqueCode, '')
WHEN b.uniqueCode IS NULL OR b.uniqueCode = ''
THEN b.name
ELSE b.uniqueCode + '(' + b.name + ')'
END AS sd
, a.specialization
...
or to remove leading and trailing spaces:
CASE
WHEN b.name IS NULL OR RTRIM(LTRIM(b.name)) = ''
THEN COALESCE(RTRIM(LTRIM(b.uniqueCode)), '')
WHEN b.uniqueCode IS NULL OR RTRIM(LTRIM(b.uniqueCode)) = ''
THEN RTRIM(LTRIM(b.name))
ELSE RTRIM(LTRIM(b.uniqueCode)) + '('
+ RTRIM(LTRIM(b.name)) + ')'
END AS sd

Compatible SQL to test for not null and not empty strings

I want to have compatible SQL for both Oracle database and Microsoft SQL server.
I want a compatible SQL expression that will return true for not null and not empty strings.
If I use:
column <> ''
it will work on Microsoft SQL server but not on Oracle database (as '' is null for Oracle)
If I use:
len(column) > 0
it will work on Microsoft SQL server but not on Oracle database (since it uses length() )
NULLIF is available on both Oracle (doc) and SQL Server (doc). This expression should work:
NULLIF(column, '') IS NOT NULL
In both servers, if column is NULL, then the output of NULLIF will just pass the NULL value through. On SQL Server, '' = '', so the output of NULLIF will be NULL. On Oracle, '' is already NULL, so it gets passed through.
This is my test on SQL Server 2008 R2 Express:
WITH SampleData AS
(SELECT 1 AS col1, NULL AS col2
UNION ALL
SELECT 2, ''
UNION ALL
SELECT 3, 'hello')
SELECT *
FROM SampleData
WHERE NULLIF(col2, '') IS NOT NULL;
And this is my test case on Oracle 10g XE:
WITH SampleData AS
(SELECT 1 AS col1, NULL AS col2 FROM DUAL
UNION ALL
SELECT 2, '' FROM DUAL
UNION ALL
SELECT 3, 'hello' FROM DUAL)
SELECT *
FROM SampleData
WHERE NULLIF(col2, '') IS NOT NULL;
Both return 3 as expected.
How about
CASE WHEN column = '' THEN NULL ELSE column END IS NOT NULL
I think the key here is to differentiate between the case when the empty string is equivalent to NULL and when it isn't:
WHERE CASE WHEN '' = '' THEN -- e.g., SQL Server this is true
CASE WHEN col <> '' AND col IS NOT NULL THEN 'Y'
ELSE 'N'
END
WHEN COALESCE(col,NULL) IS NOT NULL THEN 'Y' -- Not SS, e.g., Oracle
ELSE 'N'
END = 'Y';
If the first case is true then empty string is not the same as null, and we have to test for both string being not null and string not being the empty string. Otherwise, our task is easier because empty string and null evaluate the same.
A try to shorten #DCookie's answer. I like his ( '' = '' ) test.
CASE WHEN ( '' = '' ) THEN ( column <> '' )
ELSE ( column = column )
END
Sadly, the above will not work. The next works in SQL-Server. I can't test in Oracle now:
CASE WHEN '' = '' THEN CASE WHEN column <> '' THEN 1 ELSE NULL END
ELSE CASE WHEN column = column THEN 1 ELSE NULL END
END
which can be written also as:
( '' = '' AND column <> '' )
OR ( '' IS NULL AND column = column )