T SQL Conditional String Concatenation - sql

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]

Related

Filter data by keywords and note where they were found

so i have a query at the moment.
SELECT *
FROM TABLE1
It yields besides other stuff 4 colums which contain text "Textbox1" "Textbox2" "Textbox3" "Textbox4".
I now want to filter this query by keywords in those text boxes. Basically the user can enters 0 to 5 keywords and the rows from TABLE1 should only show if at least one keyword was found in one of the textboxes. Also a new column should be added that for every keyword that was found in a textbox should say which keyword and in which textboxes it was found.
The query itself will later run in SSRS which is where the user puts in the keywords. So the keywords are accasable as varchars. For the query you can use this:
DECLARE #Keyword1 AS VARCHAR(100) = '';
DECLARE #Keyword2 AS VARCHAR(100) = '';
DECLARE #Keyword3 AS VARCHAR(100) = '';
DECLARE #Keyword4 AS VARCHAR(100) = '';
DECLARE #Keyword5 AS VARCHAR(100) = '';
Thanks for any help.
Edit:
This is a very unelegant solution and it's not exactly what you wanted but hopefully close enough.
To improve it, especially if you are goning to extend this to more column/keywords I would consider unpivoting the data, testing each row and then aggregating the result of that back up again.
DECLARE #t TABLE (T1 varchar(100), T2 varchar(100), T3 varchar(100), T4 varchar(100))
INSERT INTO #t VALUES
(1, 11, 203, 30),
(8898, 54452, 1222, 12122)
DECLARE #k1 varchar(100) = '1'
DECLARE #k2 varchar(100) = '22'
SET #k1 = '%' + #k1 + '%'
SET #k2 = '%' + #k2 + '%'
SELECT
*
, CASE WHEN T1 like #k1 THEN '1:T1 ' ELSE '' END +
CASE WHEN T2 like #k1 THEN '1:T2 ' ELSE '' END +
CASE WHEN T3 like #k1 THEN '1:T3 ' ELSE '' END +
CASE WHEN T4 like #k1 THEN '1:T4 ' ELSE '' END +
CASE WHEN T1 like #k2 THEN '2:T1 ' ELSE '' END +
CASE WHEN T2 like #k2 THEN '2:T2 ' ELSE '' END +
CASE WHEN T3 like #k2 THEN '2:T3 ' ELSE '' END +
CASE WHEN T4 like #k2 THEN '2:T4 ' ELSE '' END
As Found
FROM #t
This give the following results.
With the help of Alan Schofield i found the solution that i need.
SELECT * from( Select*,
CASE WHEN Textbox1 like '%'+#Keyword1+'%' AND #Keyword1 <> '' THEN #Keyword1 +', ' ELSE '' END +
CASE WHEN Textbox1 like '%'+#Keyword2+'%' AND #Keyword2 <> '' THEN #Keyword2 +', ' ELSE '' END +
CASE WHEN Textbox1 like '%'+#Keyword3+'%' AND #Keyword3 <> '' THEN #Keyword3 +', ' ELSE '' END +
CASE WHEN Textbox1 like '%'+#Keyword4+'%' AND #Keyword4 <> '' THEN #Keyword4 +', ' ELSE '' END +
CASE WHEN Textbox1 like '%'+#Keyword5+'%' AND #Keyword5 <> '' THEN #Keyword5 +', ' ELSE '' END
As Textbox1found,
CASE WHEN Textbox2 like '%'+#Keyword1+'%' AND #Keyword1 <> '' THEN #Keyword1 +', ' ELSE '' END +
CASE WHEN Textbox2 like '%'+#Keyword2+'%' AND #Keyword2 <> '' THEN #Keyword2 +', ' ELSE '' END +
CASE WHEN Textbox2 like '%'+#Keyword3+'%' AND #Keyword3 <> '' THEN #Keyword3 +', ' ELSE '' END +
CASE WHEN Textbox2 like '%'+#Keyword4+'%' AND #Keyword4 <> '' THEN #Keyword4 +', ' ELSE '' END +
CASE WHEN Textbox2 like '%'+#Keyword5+'%' AND #Keyword5 <> '' THEN #Keyword5 +', ' ELSE '' END
As Textbox2found,
CASE WHEN Textbox3 like '%'+#Keyword1+'%' AND #Keyword1 <> '' THEN #Keyword1 +', ' ELSE '' END +
CASE WHEN Textbox3 like '%'+#Keyword2+'%' AND #Keyword2 <> '' THEN #Keyword2 +', ' ELSE '' END +
CASE WHEN Textbox3 like '%'+#Keyword3+'%' AND #Keyword3 <> '' THEN #Keyword3 +', ' ELSE '' END +
CASE WHEN Textbox3 like '%'+#Keyword4+'%' AND #Keyword4 <> '' THEN #Keyword4 +', ' ELSE '' END +
CASE WHEN Textbox3 like '%'+#Keyword5+'%' AND #Keyword5 <> '' THEN #Keyword5 +', ' ELSE '' END
As Textbox3found,
CASE WHEN Textbox4 like '%'+#Keyword1+'%' AND #Keyword1 <> '' THEN #Keyword1 +', ' ELSE '' END +
CASE WHEN Textbox4 like '%'+#Keyword2+'%' AND #Keyword2 <> '' THEN #Keyword2 +', ' ELSE '' END +
CASE WHEN Textbox4 like '%'+#Keyword3+'%' AND #Keyword3 <> '' THEN #Keyword3 +', ' ELSE '' END +
CASE WHEN Textbox4 like '%'+#Keyword4+'%' AND #Keyword4 <> '' THEN #Keyword4 +', ' ELSE '' END +
CASE WHEN Textbox4 like '%'+#Keyword5+'%' AND #Keyword5 <> '' THEN #Keyword5 +', ' ELSE '' END
AS Textbox4found
FROM Table1)as Result
WHERE Result.Textbox1found <> '' OR Result.Textbox2found <> '' OR Result.Textbox3found <> '' OR Result.Textbox4found <> '' or (#Keyword1='' AND #Keyword2='' AND #Keyword3='' AND #Keyword4='' AND #Keyword5='')
I switched to a Textbox based query which will generat for every textbox a column with the keywords found in that textbox.

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

SQL Server stored proc substitute an empty string if column is empty or null

I need to check to see if a certain coloumn in my stored proc is either empty or null.
This is a snippet of what I have right now:
SELECT * ,
CASE WHEN a.USER IS NULL
THEN b.ROLE
ELSE ISNULL(a.FirstName,'') + ' ' + (ISNULL(a.MiddleName+' ','') + ISNULL(a.LastName,'')
END AS 'CustomerName'
I am checking to see if a.MiddleName is NULL but how do I also check to see if its empty and if its empty to just insert a empty string (no space)?
Thanks
Change to
SELECT
* ,
CASE
WHEN a.USER IS NULL
THEN b.ROLE
ELSE CASE
WHEN ISNULL(a.MiddleName, '') = ''
THEN ISNULL(a.FirstName,'') + ' ' + ISNULL(a.LastName,'')
ELSE ISNULL(a.FirstName,'') + ' ' + a.MiddleName + ' ' + ISNULL(a.LastName,'')
END
END AS 'CustomerName'
Another sollution is:
SELECT * ,
CASE WHEN a.USER IS NULL
THEN b.ROLE
ELSE ISNULL(a.FirstName,'') + replace( ( ' ' + ISNULL(a.MiddleName+' ',' '),' ',' ') + ISNULL(a.LastName,'')
END AS 'CustomerName'

SQL empty strings and text formatting

I am trying to do some text formatting with sql (MS SQL server 2008). I have three variables which are either going to return empty strings or some sort of text, they will never be null. I would like the display the text in the following way:
#Var1='A' #Var2='B' #Var3='C' ==> A, B, C
#Var1='A' #Var2='' #Var3='C' ==> A, C
#Var1='' #Var2='B' #Var3='C' ==> B, C
#Var1='' #Var2='' #Var3='C' ==> C
.
.
.
#Var1='' #Var2='' #Var3='' ==>
etc
Here is a simplified version of what I have...
DECLARE #Var1 NVARCHAR(100)
DECLARE #Var2 NVARCHAR(100)
DECLARE #Var3 NVARCHAR(100)
SET #Var1 = 'A'
SET #Var2 = 'B'
SET #Var3 = 'C'
SELECT
ISNULL(NULLIF(#Var1,''), '')
+
CASE
WHEN NULLIF(#Var1,'') IS NOT NULL AND NULLIF(#Var2,'') IS NOT NULL THEN ', ' ELSE ''
END
+
ISNULL(NULLIF(#Var2,''),'')
+
CASE
WHEN NULLIF(#Var2,'') IS NOT NULL AND NULLIF(#Var3,'') IS NOT NULL THEN ', ' ELSE ''
END
+
ISNULL(NULLIF(#Var3,''),'')
I feel like I am missing some important details. Let me know if there is any clarification needed.
Try:
select case
when str is null then ''
else left(str, len(str)-1) -- Remove last comma
end
from (
select case when len(#Var1) > 0 then #Var1 + ', ' else '' end +
case when len(#Var2) > 0 then #Var2 + ', ' else '' end +
case when len(#Var3) > 0 then #Var3 + ', ' else '' end as str
) as SubQueryAlias
Or with a reverse trick to avoid the subquery:
select reverse(stuff(reverse(
case when len(#Var1) > 0 then #Var1 + ', ' else '' end +
case when len(#Var2) > 0 then #Var2 + ', ' else '' end +
case when len(#Var3) > 0 then #Var3 + ', ' else '' end
), 1, 2, ''));
The key point here is that SQL Server is not very good at text formatting :) You're infinitely better off in a client side language like C#.
Live example at SQL Fiddle.