Adding a '|' character to a case statement in a stored procedure - sql

I have the following case statement in a stored procedure:
Position = case coalesce(teamMember.JobTitle, '') when '' then '' else '<b>' + coalesce(teamMember.JobTitle, '') + '</b>' end
+ isnull(teamMember.OrganizationNameLevel1, '')
+ case when isnull(teamMember.OrganizationNameLevel2, '') <> isnull(teamMember.OrganizationNameLevel1, '') THEN ' | ' + isnull(teamMember.OrganizationNameLevel2, '') ELSE '' END
+ case when isnull(teamMember.OrganizationNameLevel3, '') <> isnull(teamMember.OrganizationNameLevel2, '') THEN ' | ' + isnull(teamMember.OrganizationNameLevel3, '') ELSE '' END
+ case when isnull(teamMember.OrganizationNameLevel4, '') <> isnull(teamMember.OrganizationNameLevel3, '') THEN ' | ' + isnull(teamMember.OrganizationNameLevel4, '') ELSE '' END
+ case when isnull(teamMember.OrganizationNameLevel5, '') <> isnull(teamMember.OrganizationNameLevel4, '') THEN ' | ' + isnull(teamMember.OrganizationNameLevel5, '') ELSE '' END
+ case when isnull(teamMember.OrganizationNameLevel6, '') <> isnull(teamMember.OrganizationNameLevel5, '') THEN ' | ' + isnull(teamMember.OrganizationNameLevel6, '') ELSE '' END,
It produces the following result:
<b>APPS SYSTEMS ENGINEER</b>TECH & OPS | ENT INFO TECH | DEP & OPS TECH | HR SYS & ITECH | COMP & BEN APPS
What I need is for it to show is this:
**<b>APPS SYSTEMS ENGINEER</b> |** TECH & OPS | ENT INFO TECH | DEP & OPS TECH | HR SYS & ITECH | COMP & BEN APPS

Shouldn't it be as simple as:
Position = case coalesce(teamMember.JobTitle, '') when '' then '' else '<b>' + coalesce(teamMember.JobTitle, '') + '</b>' end
+ isnull(' | ' + teamMember.OrganizationNameLevel1, '') --<-- add a Pipe here inside the ISNULL function
+ case when isnull(teamMember.OrganizationNameLevel2, '') <> isnull(teamMember.OrganizationNameLevel1, '') THEN ' | ' + isnull(teamMember.OrganizationNameLevel2, '') ELSE '' END
+ .........

Related

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.

Looking to optimize multiple JOINs into one dynamic JOIN - Possible?

So I currently have a query that pulls in buyer and seller names into a single column value. I am utilizing a CASE statement for if one of the buyer or seller values is NULL, and also since the user can delete the buyer or seller and it retains its original Sequence value (1, 2, 3, etc..) So for instance, say I have the following entries in the BuyerSeller table:
OrdersID | Sequence | BuyerSellerType | FormalName
------------------------------------------------------
1 | 1 | 0 | Billy Bob
1 | 2 | 0 | Sally Sue
1 | 1 | 1 | Joe Dirt
1 | 3 | 1 | Dwayne Johnson
My SELECT statement below will return the following:
BuyerFormalName | SellerFormalName
-----------------------------------------------
Billy Bob, Sally Sue | Joe Dirt, Dwayne Johnson
Here's the statement. Now, I am just trying to see if there is any way to dynamically or more easily produce the same results without the need for so many JOINs? I am getting the results I want, but I am looking to fine tune my skills and knowledge, especially since I might have to potentially add >3 more JOINs. Any insight would be very much appreciated!
SELECT
CASE
WHEN ISNULL(B.FormalName,'') <> '' AND ISNULL(B2.FormalName,'') <> '' AND ISNULL(B3.FormalName,'') <> ''
THEN B.FormalName + ', ' + B2.FormalName + ', ' + B3.FormalName
WHEN ISNULL(B.FormalName,'') <> '' AND ISNULL(B2.FormalName,'') = '' AND ISNULL(B3.FormalName,'') <> ''
THEN B.FormalName + ', ' + B3.FormalName
WHEN ISNULL(B.FormalName,'') <> '' AND ISNULL(B2.FormalName,'') <> '' AND ISNULL(B3.FormalName,'') = ''
THEN B.FormalName + ', ' + B2.FormalName
WHEN ISNULL(B.FormalName,'') = '' AND ISNULL(B2.FormalName,'') <> '' AND ISNULL(B3.FormalName,'') <> ''
THEN B2.FormalName + ', ' + B3.FormalName
WHEN ISNULL(B.FormalName,'') = '' AND ISNULL(B2.FormalName,'') <> '' AND ISNULL(B3.FormalName,'') = ''
THEN B2.FormalName
WHEN ISNULL(B.FormalName,'') = '' AND ISNULL(B2.FormalName,'') = '' AND ISNULL(B3.FormalName,'') <> ''
THEN B3.FormalName
ELSE B.FormalName
END AS 'BuyerFormalName'
,CASE
WHEN ISNULL(S.FormalName,'') <> '' AND ISNULL(S2.FormalName,'') <> '' AND ISNULL(S3.FormalName,'') <> ''
THEN S.FormalName + ', ' + S2.FormalName + ', ' + S3.FormalName
WHEN ISNULL(S.FormalName,'') <> '' AND ISNULL(S2.FormalName,'') = '' AND ISNULL(S3.FormalName,'') <> ''
THEN S.FormalName + ', ' + S3.FormalName
WHEN ISNULL(S.FormalName,'') <> '' AND ISNULL(S2.FormalName,'') <> '' AND ISNULL(S3.FormalName,'') = ''
THEN S.FormalName + ', ' + S2.FormalName
WHEN ISNULL(S.FormalName,'') = '' AND ISNULL(S2.FormalName,'') <> '' AND ISNULL(S3.FormalName,'') <> ''
THEN S2.FormalName + ', ' + S3.FormalName
WHEN ISNULL(S.FormalName,'') = '' AND ISNULL(S2.FormalName,'') <> '' AND ISNULL(S3.FormalName,'') = ''
THEN S2.FormalName
WHEN ISNULL(S.FormalName,'') = '' AND ISNULL(S2.FormalName,'') = '' AND ISNULL(S3.FormalName,'') <> ''
THEN S3.FormalName
ELSE S.FormalName
END AS 'SellerFormalName'
FROM
Checks C
LEFT JOIN BuyerSeller B
ON C.OrdersID = B.OrdersID
AND B.Sequence = 1
AND B.BuyerSellerType = 0
LEFT JOIN BuyerSeller B2
ON C.OrdersID = B2.OrdersID
AND B2.Sequence = 2
AND B2.BuyerSellerType = 0
LEFT JOIN BuyerSeller B3
ON C.OrdersID = B3.OrdersID
AND B3.Sequence = 3
AND B3.BuyerSellerType = 0
LEFT JOIN BuyerSeller S
ON C.OrdersID = S.OrdersID
AND S.Sequence = 1
AND S.BuyerSellerType = 1
LEFT JOIN BuyerSeller S2
ON C.OrdersID = S2.OrdersID
AND S2.Sequence = 2
AND S2.BuyerSellerType = 1
LEFT JOIN BuyerSeller S3
ON C.OrdersID = S3.OrdersID
AND S3.Sequence = 3
AND S3.BuyerSellerType = 1
Are you looking for something like this?
WITH BuyerSeller
AS
(
SELECT *
FROM
(
VALUES (1,1,0,'Billy Bob'),
(1,2,0,'Sally Sue'),
(1,1,1,'Joe Dirt'),
(1,3,1,'Dwayne Johnson')
) A(OrdersID,[Sequence],BuyerSellerType,FormalName)
)
SELECT DISTINCT OrdersID,
STUFF(Bname,1,2,'') AS BuyerFormalName,
STUFF(Sname,1,2,'') AS SellerFormalName
FROM BuyerSeller A
CROSS APPLY (
SELECT ', ' + FormalName
FROM BuyerSeller B
WHERE A.OrdersID = B.OrdersID
AND BuyerSellerType = 0
ORDER BY [Sequence]
FOR XML PATH('')
) CA(Bname)
CROSS APPLY (
SELECT ', ' + FormalName
FROM BuyerSeller B
WHERE A.OrdersID = B.OrdersID
AND BuyerSellerType = 1
ORDER BY [Sequence]
FOR XML PATH('')
) CA2(Sname)
Results:
OrdersID BuyerFormalName SellerFormalName
----------- --------------------- -----------------------
1 Billy Bob, Sally Sue Joe Dirt, Dwayne Johnson
I still prefer old-style MAX/CASE in such a case if the number of rows/values is known (and small):
SELECT
c.OrdersID,
BuyerFormalName,
SellerFormalName
FROM Checks C
LEFT JOIN
(
SELECT
OrdersID,
STUFF(MAX(CASE WHEN Sequence = 1 AND BuyerSellerType = 0 THEN ', ' + FormalName ELSE '' end) +
MAX(CASE WHEN Sequence = 2 AND BuyerSellerType = 0 THEN ', ' + FormalName ELSE '' end) +
MAX(CASE WHEN Sequence = 3 AND BuyerSellerType = 0 THEN ', ' + FormalName ELSE '' end), 1,2,'') AS BuyerFormalName,
STUFF(MAX(CASE WHEN Sequence = 1 AND BuyerSellerType = 1 THEN ', ' + FormalName ELSE '' end) +
MAX(CASE WHEN Sequence = 2 AND BuyerSellerType = 1 THEN ', ' + FormalName ELSE '' end) +
MAX(CASE WHEN Sequence = 3 AND BuyerSellerType = 1 THEN ', ' + FormalName ELSE '' end), 1,2,'') AS SellerFormalName
FROM BuyerSeller
GROUP BY OrdersID
) AS b
ON C.OrdersID = B.OrdersID
;

How to concatenate strings and commas in SQL Server?

I'm relatively new to MSSQL, so sorry if the question might sounds trivial. I want to concatenate multiple fields with a delimiter ,. However, when the field is empty, the extra , will be included in the result string as well. So is there an easy way to solve this problem? For example,
SELECT VRI.Street_Number_and_Modifier + ',' +
VRI.Street_Direction + ',' +
VRI.Street_Name + ',' +
VRI.Street_Direction + ',' +
VRI.Street_Suffix + ',' +
VRI.Street_Post_Direction + ',' +
VRI.Unit
FROM View_Report_Information_Tables VRI
This modified version of Lamak's handles NULL or strings containing only space/empty:
SELECT COALESCE(NULLIF(VRI.Street_Number_and_Modifier, '') + ',', '') +
COALESCE(NULLIF(VRI.Street_Direction, '') + ',', '') +
COALESCE(NULLIF(VRI.Street_Name, '') + ',', '') +
COALESCE(NULLIF(VRI.Street_Direction, '') + ',', '') +
COALESCE(NULLIF(VRI.Street_Suffix, '') + ',', '') +
COALESCE(NULLIF(VRI.Street_Post_Direction, '') + ',', '') +
COALESCE(NULLIF(VRI.Unit, ''), '')
FROM View_Report_Information_Tables VRI
I was able to get it to work with a slightly different approach. Putting the commas at the beginning of each field and then removing the first one with the STUFF function worked for me:
SELECT
STUFF((COALESCE(', ' + NULLIF(VRI.Street_Number_and_Modifier, ''), '') +
COALESCE(', ' + NULLIF(VRI.Street_Direction, ''), '') +
COALESCE(', ' + NULLIF(VRI.Street_Name, ''), '')) +
COALESCE(', ' + NULLIF(VRI.Street_Direction, ''), '')) +
COALESCE(', ' + NULLIF(VRI.Street_Suffix, ''), '')) +
COALESCE(', ' + NULLIF(VRI.Street_Post_Direction, ''), '')) +
COALESCE(', ' + NULLIF(VRI.Unit, ''), ''))
, 1, 2, '')
FROM View_Report_Information_Tables AS VRI
If the columns are empty instead of null, you can try this:
SELECT VRI.Street_Number_and_Modifier
+ CASE WHEN VRI.Street_Number_and_Modifier <> '' THEN ', ' ELSE '' END
+ VRI.Street_Direction
+ CASE WHEN VRI.Street_Direction <> '' THEN ', ' ELSE '' END
+ VRI.Street_Name
+ CASE WHEN VRI.Street_Name <> '' THEN ', ' ELSE '' END
+ VRI.Street_Direction
+ CASE WHEN VRI.Street_Direction <> '' THEN ', ' ELSE '' END
+ VRI.Street_Suffix
+ CASE WHEN VRI.Street_Suffix <> '' THEN ', ' ELSE '' END
+ VRI.Street_Post_Direction
+ CASE WHEN VRI.Street_Post_Direction <> '' THEN ', ' ELSE '' END
+ VRI.Unit
+ CASE WHEN VRI.Unit<> '' THEN ', ' ELSE '' END
FROM View_Report_Information_Tables VRI
For SQL 2008+
Using
ISNULL(Colmn1 + ', ', '')
Will always result with a leading comma in the end, so you'll have to handle it.
Example:
DECLARE #Column1 NVARCHAR(10) = 'Column1'
, #Column2 NVARCHAR(10) = 'Column2'
SELECT SUBSTRING( ISNULL(#Column1 + ', ', '') + ISNULL(#Column2 + ', ', '')
, 0 --Starting from 0 not 1 to remove leading comma
, LEN(ISNULL(#Column1 + ', ', '') + ISNULL(#Column2 + ', ', '')))
Or we could approach this the other way around and use the STUFF function to remove our beginning comma which looks cleaner, example:
SELECT STUFF (ISNULL(( ', ' + #Column1), '') + ISNULL(( ', ' + #Column2), ''), 1, 2, N'')
For SQL 2012+ we could use the CONCAT function and remove beginning comma using STUFF similar to our previous example but avoiding ISNULL:
SELECT STUFF(CONCAT( ', ' + #Column1, ', ' + #Column2), 1, 2, N'')
For SQL 2017+ CONCAT_WS was introduced where you can concatinate/join multiple string columns with a delimiter specified in the first argument of the function:
MS Documents CONCAT_WS
MS Doc Example:
SELECT CONCAT_WS(',' --delimiter
,'1 Microsoft Way', NULL, NULL, 'Redmond', 'WA', 98052) AS Address;
Try this:
SELECT COALESCE(VRI.Street_Number_and_Modifier + ',','') +
COALESCE(VRI.Street_Direction + ',','') +
COALESCE(VRI.Street_Name + ',','') +
COALESCE(VRI.Street_Direction + ',','') +
COALESCE(VRI.Street_Suffix + ',','') +
COALESCE(VRI.Street_Post_Direction + ',','') +
COALESCE(VRI.Unit,'')
FROM View_Report_Information_Tables VRI
Short or long answer?
Short answer - dont. This is a formatting issue, not a database issue.
Long answer - When you concatenate a string and a null in sql server, the result is null. So you can use combinations of ISNULL
SELECT ISNULL(afield + ',','') + ISNULL(bfield + ',','')
You have to use select case when IsNull(fieldname, '')= '' or ltrim(rtrim(fieldname))='') Then ... Else... end +...
Edit:
Was written from Android mobile.
Below your example.
The following translations (from German) apply, FYI:
Vorname: given name
Name: surname
Benutzer: User
And here's the example code:
CREATE VIEW [dbo].[V_RPT_SEL_Benutzer]
AS
SELECT
BE_ID AS RPT_UID,
CASE
WHEN (ISNULL(BE_Name, '0') = '0' OR LTRIM(RTRIM(BE_Name)) = '') AND (ISNULL(BE_Vorname, '0') = '0' OR LTRIM(RTRIM(BE_Vorname)) = '')
THEN ''
WHEN (ISNULL(BE_Name, '0') = '0' OR LTRIM(RTRIM(BE_Name)) = '')
THEN ISNULL(BE_Vorname, '')
WHEN (ISNULL(BE_Vorname, '0') = '0' OR LTRIM(RTRIM(BE_Vorname)) = '')
THEN ISNULL(BE_Name, '')
ELSE
ISNULL(BE_Name, '') + ', ' + ISNULL(BE_Vorname, '')
END AS RPT_Name,
ROW_NUMBER() OVER (ORDER BY BE_Name, BE_Vorname ASC) AS RPT_Sort
FROM T_Benutzer
You could use the ISNULL(field + ',', '')
SELECT isnull(VRI.Street_Number_and_Modifier + ',','')+
isnull(VRI.Street_Direction + ',','')+
isnull(VRI.Street_Name + ',','')+
isnull(VRI.Street_Direction + ',','')+
isnull(VRI.Street_Suffix + ',','')+
isnull(VRI.Street_Post_Direction + ',','')+
isnull(VRI.Unit,'')
FROM View_Report_Information_Tables VRI
I would agree completely with Jamiec's short answer.
Otherwise, I would look at a nasty solution of using a REPLACE([concat], ',,', ',') everywhere you concatenate two columns, and then figure out how to trim commas from the beginning and end of the string where the first and last columns might be empty. Very very messy.
Wanted to see if I can get it without using CASE but could not. A long-winded way of mine:
SELECT case when isnull(nullif(VRI.Street_Number_and_Modifier, ''),'')='' then '' else VRI.Street_Number_and_Modifier end
+ case when isnull(nullif(VRI.Street_Direction, ''),'')='' then '' else ',' + VRI.Street_Direction end
+ case when isnull(nullif(VRI.Street_Name, ''),'')='' then '' else ',' + VRI.Street_Name end
+ case when isnull(nullif(VRI.Street_Suffix, ''),'')='' then '' else ',' + VRI.Street_Suffix end
+ case when isnull(nullif(VRI.Street_Post_Direction, ''),'')='' then '' else ',' + VRI.Street_Post_Direction end
+ case when isnull(nullif(VRI.Street_Post_Direction, ''),'')='' then '' else ',' + VRI.Street_Post_Direction end
FROM View_Report_Information_Tables VRI
SELECT COALESCE(NULLIF(ad.UDEFPROPERTYADDRESS_ADDRSS_LN_1_TXT, ''), ',')+
COALESCE(NULLIF(ad.UDEFPROPERTYADDRESS_ADDRSS_LN_2_TXT, '') , ',')+
COALESCE(NULLIF(ad.UDEFPROPERTYADDRESS_ADDRSS_LN_3_TXT, '') , ',')+
COALESCE(NULLIF(ad.UDEFPROPERTYADDRESS_CITY_TXT, '') , ',')+
COALESCE(NULLIF(ad.UDEFPROPERTYADDRESS_ST_TXT, '') , ',')+
COALESCE(NULLIF(ad.UDEFPROPERTYADDRESS_CNTRY_TXT, '') , ',')+
COALESCE(NULLIF(ad.UDEFPROPERTYADDRESS_PSTL_CD, '') , '')
FROM ACCOUNT_DETAILS ad
This will not add any commas if null-strings
SELECT CONCAT_WS(', ', IFNULL(column1, NULL),
IFNULL(column2, NULL), IFNULL(column3, NULL),
IFNULL(column4, NULL), IFNULL(column5, NULL))
FROM yourtable

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]