Why is the field empty when I use a multiple case when? - sql

I have this SQL Code:
SELECT ( P1.Name
+ (CASE P1.Vorname WHEN '' THEN '' ELSE ', ' + P1.Vorname END)
+ (CASE (AP1.Postleitzahl + AP1.Ort)
WHEN '' THEN ''
ELSE ' - ' + (AP1.Postleitzahl + AP1.Ort)
END)
+ (CASE (PKT1.Kennung + PKM1.Kennung)
WHEN ''
THEN
''
ELSE
' - '
+ (CASE PKT1.Kennung
WHEN '' THEN ''
ELSE 'Tel: ' + PKT1.Kennung
END)
+ (CASE PKM1.Kennung
WHEN '' THEN ''
ELSE ' Mobil: ' + PKM1.Kennung
END)
END))
AS [IO1] FROM XYZ
Now when one field is not filled, the cell is empty.
When everything is filled with data the cell gets filled.
So what is the Problem with that code?
I have to work with MSSQL Server 2005 so CONCAT or whatever will not work.

This is your query:
SELECT (P1.Name +
(CASE P1.Vorname WHEN '' THEN '' ELSE ', ' + P1.Vorname END) +
(CASE (AP1.Postleitzahl + AP1.Ort) WHEN '' THEN '' ELSE ' - ' + (AP1.Postleitzahl + AP1.Ort) END) +
(CASE (PKT1.Kennung + PKM1.Kennung) WHEN '' THEN '' ELSE ' - ' + (CASE PKT1.Kennung WHEN '' THEN '' ELSE 'Tel: ' + PKT1.Kennung END) +
(CASE PKM1.Kennung WHEN '' THEN '' ELSE ' Mobil: ' + PKM1.Kennung END) END)
) AS [IO1]
FROM XYZ;
It is going to produce NULL if any of the arguments are NULL. Presumably one or more of the columns is NULL. You can fix this by wrapping things in coalesce() and/or changing the case statements. For instance:
SELECT (coalesce(P1.Name, '') +
(CASE WHEN P1.Vorname = '' or P1.Vorname is null THEN '' ELSE ', ' + P1.Vorname END) +
(CASE WHEN (AP1.Postleitzahl + AP1.Ort) = '' or (AP1.Postleitzahl + AP1.Ort) is null THEN '' ELSE ' - ' + (AP1.Postleitzahl + AP1.Ort) END) +
(CASE WHEN (PKT1.Kennung + PKM1.Kennung) = '' or (PKT1.Kennung + PKM1.Kennung) is null THEN '' ELSE ' - ' + (CASE WHEN PKT1.Kennung = '' or PKT1.Kennung is null THEN '' ELSE 'Tel: ' + PKT1.Kennung END) +
(CASE WHEN PKM1.Kennung = '' or PKM1.Kennung is null THEN '' ELSE ' Mobil: ' + PKM1.Kennung END) END)
) AS [IO1]
FROM XYZ;

Related

Rolling multiple data rows into 1 row with 'NA' or 'None' displayed if NULL

I am using the below statement to retrieve values in one line.
I would like to display 'NA' or 'None' if there is no Value.
How do I include this in the below statement?
STUFF((SELECT CASE WHEN COUNT(pp.sPetType) > 0 and pp.sPetIsServiceAnimal = 'Yes' THEN
CASE WHEN COUNT(pp.sPetType) = 1 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '1 ' + pp.sPetType ELSE
CASE WHEN COUNT(pp.sPetType) = 2 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '2 ' + pp.sPetType + 's' ELSE
CASE WHEN COUNT(pp.sPetType) = 3 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '3 ' + pp.sPetType + 's'
END END END ELSE 'None' END
FROM person_pet pp
WHERE t.hMyPerson = pp.hMyPerson
GROUP BY pp.sPetType, pp.sPetIsServiceAnimal
FOR XML Path('')), 1, 1, '') [ServiceAnimal]
If I understood correctly :
Add another CASE in the outer case like this:
CASE WHEN (pp.sPetType IS NULL OR pp.sPetType = '') THEN 'NA'
STUFF((SELECT ISNULL(CASE WHEN COUNT(pp.sPetType) > 0 and pp.sPetIsServiceAnimal = 'Yes' THEN
CASE WHEN COUNT(pp.sPetType) = 1 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '1 ' + pp.sPetType ELSE
CASE WHEN COUNT(pp.sPetType) = 2 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '2 ' + pp.sPetType + 's' ELSE
CASE WHEN COUNT(pp.sPetType) = 3 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '3 ' + pp.sPetType + 's'
END END END ELSE 'None' END,'None')
FROM person_pet pp
WHERE t.hMyPerson = pp.hMyPerson
GROUP BY pp.sPetType, pp.sPetIsServiceAnimal
FOR XML Path('')), 1, 1, '') [ServiceAnimal]
I am assuming you are wanting the [ServiceAnimal] to have a result of 'NA' if it is null. This is a simple way to achieve that.
ISNULL(STUFF((SELECT CASE WHEN COUNT(pp.sPetType) > 0 and pp.sPetIsServiceAnimal = 'Yes' THEN
CASE WHEN COUNT(pp.sPetType) = 1 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '1 ' + pp.sPetType ELSE
CASE WHEN COUNT(pp.sPetType) = 2 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '2 ' + pp.sPetType + 's' ELSE
CASE WHEN COUNT(pp.sPetType) = 3 and pp.sPetIsServiceAnimal = 'Yes' then '; ' + '3 ' + pp.sPetType + 's'
END END END ELSE 'None' END
FROM person_pet pp
WHERE t.hMyPerson = pp.hMyPerson
GROUP BY pp.sPetType, pp.sPetIsServiceAnimal
FOR XML Path('')), 1, 1, ''), 'NA') [ServiceAnimal]

How do I remove a trailing comma when an alias is involved?

CASE WHEN Col1 = 1 THEN
(CASE WHEN Col2=2 THEN 'a' + ', ' END) +
(CASE WHEN Col3=2 THEN 'b' + ', ' END) +
(CASE WHEN Col4=2 THEN 'c' END)
END as Names
Names can be a,b,c or a,b, or b,
How do I remove the character "," at the end? I cannot use replace or stuff etc functions as "Names" is an alias
original Query:
CASE WHEN ht.bTax=0 then
ISNULL(CASE WHEN (r1.iOccType=2) THEN p1.SFIRSTNAME + ' ' + p1.ULASTNAME END,'') +
ISNULL(CASE WHEN (r2.iOcctType=2) THEN p2.SFIRSTNAME + ' ' + p2.ULASTNAME + ', ' END, '') +
ISNULL(CASE WHEN (r3.iOccType=2) THEN p3.SFIRSTNAME + ' ' + p3.ULASTNAME + ', ' END, '') +
ISNULL(CASE WHEN (r4.iOccType=2) THEN p4.SFIRSTNAME + ' ' + p4.ULASTNAME + ', ' END, '') +
ISNULL(CASE WHEN (r5.iOccType=2) THEN p5.SFIRSTNAME + ' ' + p5.ULASTNAME + ', ' END, '')
End AS Names
My solution would be to add the comma to the end of the 'c' result exactly the same as the others then remove the last two characters from the string after the fact. I think it would be cleaner to put your first statement into a cte then select from it while cleaning up the string then:
with cte as(
Select
CASE WHEN 1 = 1 THEN
(CASE WHEN Col2=2 THEN 'a, ' ELSE '' END) +
(CASE WHEN Col3=2 THEN 'b, ' ELSE '' END) +
(CASE WHEN Col3=2 THEN 'c, ' ELSE '' END)
END as Names
)
select SUBSTRING(Names, 0, Len(Names)) as Names from CTE
Alternatively you can just do it inline but you'll have to repeat your string construction a second time this way.
CASE WHEN Col1 = 1 THEN
SUBSTRING(
(CASE WHEN Col2=2 THEN 'a, ' ELSE '' END) +
(CASE WHEN Col3=2 THEN 'b, ' ELSE '' END) +
(CASE WHEN Col4=2 THEN 'c, ' ELSE '' END)
,0, LEN(
(CASE WHEN Col2=2 THEN 'a, ' ELSE '' END) +
(CASE WHEN Col3=2 THEN 'b, ' ELSE '' END) +
(CASE WHEN Col4=2 THEN 'c, ' ELSE '' END)
))
END as Names
Just to explain what I'm doing here:
SUBSTRING (string, start point, end point) - returns contents of string from start point (0 is beginning) and the end point
LEN (string) - returns the amount of characters in the string
By passing 0 as the start and the LEN() of the string as the last parameter, it will return the full string missing the last character (because the index starts at zero). Usually you would have to put LEN() - 1 but apparently LEN does not count the last empty character.

If true do something else skip SQL

Hello, I have data that looks like this:
I'm trying to come with the code that will calculate the 'MERGE' column.
Basically, I should check, if CLM_x >0 then take the value from SZ_x and concat with the value in CLM_x.
I'm trying to use case when, however I don't know how to skip merging if CLM_x =0:
CASE WHEN CLM_TBL1 > 0 THEN ('Size ' + SZ_1 + '-Qty '+CLM_1) else ... end ...
Please advise, Thank you!
Yuck. This is a bunch of string arithmetic:
select stuff( ((case when clm1_1 > 0 then concat(', Size ', sz_1, '-Qty ', clm1_1) else '' end) +
(case when clm1_2 > 0 then concat(', Size ', sz_2, '-Qty ', clm1_2) else '' end) +
(case when clm1_3 > 0 then concat(', Size ', sz_3, '-Qty ', clm1_3) else '' end)
), 1, 2, ''
) as merge_column
You just need to string the case statements together.
merge =
case
when CLM_1 > 0 then 'Size ' + SZ_1 + '-Qty '+ CLM_1 + ' '
else ''
end
+
case
when CLM_2 > 0 then 'Size ' + SZ_2 + '-Qty '+ CLM_2 + ' '
else ''
end
+
case
when CLM_3 > 0 then 'Size ' + SZ_3 + '-Qty '+ CLM_3 + ' '
else ''
end

RTRIM & LTRIM Function with Case Statement

How do i use the LTRIM & RTRIM with the following SQL? I need to LTRIM and RTRIM all these fields for leading spaces
UPDATE CORE.WeccoPartyAddress
SET AddressElements = CONTROL.TrimChar(
CASE when COALESCE(Address1,'') != '' THEN Address1 + ', ' ELSE '' END +
CASE when COALESCE(Address2,'') != '' THEN Address2 + ', ' ELSE '' END +
CASE when COALESCE(Address3,'') != '' THEN Address3 + ', ' ELSE '' END +
CASE when COALESCE(Town,'') != '' THEN Town + ', ' ELSE '' END +
CASE when COALESCE(County,'') != '' THEN County + ', ' ELSE '' END +
CASE when COALESCE(Postcode,'') != '' THEN Postcode ELSE '' END, ', '
)
Use like below nested:
UPDATE CORE.WeccoPartyAddress SET AddressElements = rtrim(ltrim(CASE when COALESCE(Address1,'') != '' THEN Address1 + ', ' ELSE '' END + CASE when COALESCE(Address2,'') != '' THEN Address2 + ', ' ELSE '' END + CASE when COALESCE(Address3,'') != '' THEN Address3 + ', ' ELSE '' END + CASE when COALESCE(Town,'') != '' THEN Town + ', ' ELSE '' END + CASE when COALESCE(County,'') != '' THEN County + ', ' ELSE '' END + CASE when COALESCE(Postcode,'') != '' THEN Postcode ELSE '' END))
You don't have to use CASE in your statement
UPDATE CORE.WeccoPartyAddress
SET AddressElements = ISNULL( STUFF (
COALESCE( ', ' + LTRIM( RTRIM(Address1) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Address2) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Address3) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Town) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1County) ) , '') +
COALESCE( ', ' + LTRIM( RTRIM(Address1Postcode) ) , '')
,1
,2
,''
), '')
If any of Address values is not null you will get string like this: ', Address', then using the function STUFF you replace ', ' at the beginning of the string to get 'Address' as the result.
If all values are null the STUFF function will return NULL which will be replaced with '' by ISNULL function.

T SQL Conditional String Concatenation

Have a 5 columns of address data. I need to concatenate these fields into a single address with spaces in between the values if they exist. If the column has a null value I should skip it and not enter any space.
select
case
when street_number != '' THEN (cast(street_number as int))
end as street_number,
case
when street_ext != '' then
case
when street_ext = 50 then '1/2'
end
end as street_ext,
case
when street_direct ! = '' then street_direct
end as street_direct,
case
when site_street ! = '' then site_street
end as site_street,
case
when site_address ! = '' then site_address
end as site_address
from parcel
what I'd like to do is have a variable and assign it to the value of the first column street_number, then when I move on to the next column, street_ext, if it isn't null I'd like to check to see if the variable is null and if not, append a space and the value...and so on down the road.
I'm rusty as hell and could use a push in the right direction.
Thanks everyone.
Use the "+" to concatenate strings in TSQL:
SELECT CASE
WHEN LEN(p.street_number) > 0 THEN p.street_number + ' '
ELSE ''
END +
CASE
WHEN p.street_ext = 50 THEN '1/2'
WHEN LEN(p.street_ext) > 0 THEN ''
ELSE p.street_ext
END + ' ' +
CASE
WHEN LEN(p.street_direct) > 0 THEN p.street_direct + ' '
ELSE ''
END +
CASE
WHEN LEN(p.site_street) > 0 THEN p.site_street + ' '
ELSE ''
END +
CASE
WHEN LEN(p.site_address) > 0 THEN p.site_address + ' '
ELSE ''
END AS full_address
FROM PARCEL p
The LEN function returns zero if the string value is NULL, or a zero length string.
Nested isnulls could do what you need. Something like:
SELECT
ISNULL(streetnumber + ' ', '')
+ ISNULL(streetext + ' ', '')
etc
relying on the fact that NULL + ' ' = NULL.
Something along the lines of:
select coalesce(street_number+' ','')+
coalesce(case when street_ext=50 then '1/2' else null end+' ','')+
coalesce(street_direct+' ','')+
coalesce(site_street+' ','')+
coalesce(site_address,'')
from parcel
I have assumed your data types are all varchar or similar for simplicity. If you are OK with removing any double spaces, how about:
rtrim(ltrim(replace(isnull(street_number) + ' '
+ isnull(street_ext) + ' '
+ isnull(street_direct) + ' '
+ isnull(site_street) + ' '
+ isnull(site_address), ' ', ' ')))
First I would declare the seperator as a variable, because customers are notorious for changing these.
I would do this as follows:
DECLARE #AddressSeperator NVARCHAR(5) = ' '
...and then for the column declation, I'd use the following:
, CONCAT
(
(CASE WHEN LEN(p.street_number) > 0 THEN p.street_number + #AddressSeperator ELSE '' END)
, (CASE WHEN p.street_ext = 50 THEN '1/2' + #AddressSeperator WHEN LEN(p.street_ext) > 0 THEN p.street_ext + #AddressSeperator ELSE '' END)
, (CASE WHEN LEN(p.street_direct) > 0 THEN p.street_direct + #AddressSeperator ELSE '' END)
, (CASE WHEN LEN(p.site_street) > 0 THEN p.site_street + #AddressSeperator ELSE '' END)
, ISNULL(p.site_address, '')
) AS [full_address]