Remove Extra Comma in String Concatenation



declare @one char(1)='a'
declare @two char(1)=''
declare @three char(1)='c'
declare @four char(1)='d'

declare @s varchar(50)
 
select @s=ISNULL(nullif(@one,'')+',','')+ISNULL(nullif(@two,'')+',','')+ISNULL(nullif(@three,'')+',','')+ISNULL(nullif(@four,''),'')           

SELECT CASE
         WHEN Patindex('%,', @s) > 0 THEN Substring(@s, 1, Len(@s) - 1)
         ELSE @s
       END 


 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s