Showing posts with label concatination. Show all posts
Showing posts with label concatination. Show all posts

Sunday, February 12, 2012

Concatination Query?

I have a need to query some data and string all my results by id. I am fairly close to the results but stuck on the final piece. Any help would be greatly appreciated.

Here's the scenario: My data looks as follows:

UserID Results 1095 ,,,,,,, 1095 ,,,,,8,, 1095 ,,,,,,, 1095 ,,,,,8,, 1247 ,2,3,,,6,,, 1247 ,2,3,,,6,,, 1247 ,2,3,,,6,,, 1247 ,2,3,,,6,,, 4069 ,,,,,,, 4069 ,,,,,,, 4069 ,,,,,,, 4069 ,,,,,,, 4070 ,,6,,,,, 4070 ,,6,,,,, 4070 ,,6,,,,, 4070 ,,6,,,,,

I want to query it and end up with the results all strung together under each UserID as follows:

1095 ,,,,,,,,,,,,8,,,,,,,,,,,,,,8,, 1247 ,2,3,,,6,,,,2,3,,,6,,,,2,3,,,6,,,,2,3,,,6,,, 4069 ,,,,,,,,,,,,,,,,,,,,,,,,,,,, 4070 ,,6,,,,,,,6,,,,,,,6,,,,,,,6,,,,,

I know if I use the following code I can string all the results together

select *, Cast((SELECT Results + ',' FROM #temp1 FOR XML PATH('')) as varchar(max) ) as Results from #temp1

But I can't figure out how to break it down by individual UserID. Any help is greatly appreciated. Thanks in advance

If you are using SQL 2005, you could do something like this:

-- Create Comma Delimited list from multiple rows
-- From Tony Roberson
-- SQL 2005

DECLARE @.MailingList table
( IndividualName nvarchar(100) not null,
ListName nvarchar(10) not null
)

INSERT INTO @.MailingList VALUES( 'Bill Smith', 'List A' )
INSERT INTO @.MailingList VALUES( 'Bill Smith', 'List B' )
INSERT INTO @.MailingList VALUES( 'Bill Smith', 'List C' )
INSERT INTO @.MailingList VALUES( 'Sally Jones', 'List A' )
INSERT INTO @.MailingList VALUES( 'Sally Jones', 'List B' )
INSERT INTO @.MailingList VALUES( 'Hector Lopez', 'List A' )

SELECT DISTINCT
IndividualName,
List = substring( ( SELECT ', ' + ListName as [text()]
FROM @.MailingList m2
WHERE m2.IndividualName = m1.IndividualName
FOR XML path(''), elements
), 3, 1000
)
FROM @.MailingList m1

/*
Gives this result:

Alex R List A
Joe R List A, List B
Tony R List A, List B, List C

*/

|||

Outstanding thanks!!!

Final question, in the query

...FOR XML path(''), elements
), 3, 1000
)

what does the 3, 1000 do

|||

They are the second and third arguments for the substring function. They remove the first two characters from the final string (up to 1000 characters starting at the third).

The list is generated with ', ' in front of each member (i.e. ', a, b, c, d, e') whereas you do not want a comma/space in front of the first item so this removes the first ', ' from the front of the list.

|||Once again thanks I really appreciate the help|||

As Dhericean noted, those are parameters for the substring() function.

You could eliminate the space in [ SELECT ', ' ] and change the value 3 to 2. And you can increase or reduce the value 1000 to more closely represent the lenght of the final concatenated string.

|||Arnie -

A while back you provided me this solution to a need I had. Many thanks, I have use it often and with many tweeks. Now I am trying to do something similar and I am stuck, but I can't figure out why....

Here is our process and what is happening:

This is our preliminary query... it gets the group of raw data we will be analyzing (and it works)

SELECT SITEDATA.SITEID
, SITEDATA.ANOMALYT1 + ','
+ SITEDATA.ANOMALYT2 + ','
+ SITEDATA.ANOMALYT3 + ','
+ SITEDATA.ANOMALYT4 + ','
+ SITEDATA.ANOMALYT5 + ','
+ SITEDATA.ANOMALYT6 + ','
+ SITEDATA.ANOMALYT7 + ','
+ SITEDATA.ANOMALYT8 AS ANOMALIES
, SITEDATA.CALC_DATE, SITE.PROPID, SITE.LOCATION
INTO [#TEMP1]
FROM SITEDATA INNER JOIN SITE ON SITEDATA.SITEID = SITE.SITEID
WHERE (SITEDATA.SITEID = 907) AND (SITEDATA.CALC_DATE BETWEEN '1/1/06' AND '12/31/06')

Here is a sample of results from the above Query (Note, there are values in the Anomalies column):

SITEID Anomalies Calc_Date PropID Location
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5

Now we want to string all the data in the Anomalies column together by Distinct Calc_Date. To do this we execute the following Proc:

SELECT DISTINCT CALC_DATE, SITEID, PROPID, LOCATION, ANOMALIES = SUBSTRING( (
SELECT ',' + ANOMALIES AS [text()] FROM #TEMP1 T1 WHERE T1.SITEID = T2.SITEID FOR XML PATH(''), ELEMENTS ), 3, 1000 )
INTO #TEMP2
FROM #TEMP1 T2

This procedure strings the Anomalies together, but the data appears to be lost, i.e. we should get ,2,3,,,6,,,2,3,6,,,,2,3,,,6, etc. but instead all we get is ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

I thought the issue might be caused by date and time not being distinct, so I tried converting the date to varchar and rerunning, but no difference.... Any thoughts?|||

Jim,

Your query worked perfectly for me. (minor formatting alterations...)


SET NOCOUNT ON

DECLARE @.Temp1 table
( RowID int IDENTITY,
SiteID int,
Anomalies varchar(100),
Calc_Date datetime,
PropID int,
Location int
)

INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )

SELECT DISTINCT
Calc_Date,
SiteID,
PropID,
LOCATION,
Anomalies = substring( ( SELECT ',' + Anomalies AS [text()]
FROM @.Temp1 T2
WHERE T1.SITEID = T2.SITEID
FOR XML PATH(''), ELEMENTS
), 3, 1000
)
INTO #Temp2
FROM @.Temp1 T1

SELECT *
FROM #Temp2

Calc_Date SiteID PropID LOCATION Anomalies

-- -- --
2006-03-26 00:00:00.000 907 40 5 2,3,,,6,,,,,2,3,,,6,,,,,2,3,,,6,,,,,

(Anomalies repetition removed for display purposes...)

|||Thanks again Arnie.

I couldn't replicate your success, however I figured out a different query to and up with the same results. Because the DS is small, I just searched the Anomalies field without combining them, then I looked for distinct date. I think it ended up being faster anyway

Concatination Query?

I have a need to query some data and string all my results by id. I am fairly close to the results but stuck on the final piece. Any help would be greatly appreciated.

Here's the scenario: My data looks as follows:

UserID

Results

1095

,,,,,,,

1095

,,,,,8,,

1095

,,,,,,,

1095

,,,,,8,,

1247

,2,3,,,6,,,

1247

,2,3,,,6,,,

1247

,2,3,,,6,,,

1247

,2,3,,,6,,,

4069

,,,,,,,

4069

,,,,,,,

4069

,,,,,,,

4069

,,,,,,,

4070

,,6,,,,,

4070

,,6,,,,,

4070

,,6,,,,,

4070

,,6,,,,,

I want to query it and end up with the results all strung together under each UserID as follows:

1095

,,,,,,,,,,,,8,,,,,,,,,,,,,,8,,

1247

,2,3,,,6,,,,2,3,,,6,,,,2,3,,,6,,,,2,3,,,6,,,

4069

,,,,,,,,,,,,,,,,,,,,,,,,,,,,

4070

,,6,,,,,,,6,,,,,,,6,,,,,,,6,,,,,

I know if I use the following code I can string all the results together

select *, Cast((SELECT Results + ',' FROM #temp1 FOR XML PATH('')) as varchar(max) ) as Results from #temp1

But I can't figure out how to break it down by individual UserID. Any help is greatly appreciated. Thanks in advance

If you are using SQL 2005, you could do something like this:

-- Create Comma Delimited list from multiple rows
-- From Tony Roberson
-- SQL 2005

DECLARE @.MailingList table
( IndividualName nvarchar(100) not null,
ListName nvarchar(10) not null
)

INSERT INTO @.MailingList VALUES( 'Bill Smith', 'List A' )
INSERT INTO @.MailingList VALUES( 'Bill Smith', 'List B' )
INSERT INTO @.MailingList VALUES( 'Bill Smith', 'List C' )
INSERT INTO @.MailingList VALUES( 'Sally Jones', 'List A' )
INSERT INTO @.MailingList VALUES( 'Sally Jones', 'List B' )
INSERT INTO @.MailingList VALUES( 'Hector Lopez', 'List A' )

SELECT DISTINCT
IndividualName,
List = substring( ( SELECT ', ' + ListName as [text()]
FROM @.MailingList m2
WHERE m2.IndividualName = m1.IndividualName
FOR XML path(''), elements
), 3, 1000
)
FROM @.MailingList m1

/*
Gives this result:

Alex R List A
Joe R List A, List B
Tony R List A, List B, List C

*/

|||

Outstanding thanks!!!

Final question, in the query

...FOR XML path(''), elements
), 3, 1000
)

what does the 3, 1000 do

|||

They are the second and third arguments for the substring function. They remove the first two characters from the final string (up to 1000 characters starting at the third).

The list is generated with ', ' in front of each member (i.e. ', a, b, c, d, e') whereas you do not want a comma/space in front of the first item so this removes the first ', ' from the front of the list.

|||Once again thanks I really appreciate the help|||

As Dhericean noted, those are parameters for the substring() function.

You could eliminate the space in [ SELECT ', ' ] and change the value 3 to 2. And you can increase or reduce the value 1000 to more closely represent the lenght of the final concatenated string.

|||Arnie -

A while back you provided me this solution to a need I had. Many thanks, I have use it often and with many tweeks. Now I am trying to do something similar and I am stuck, but I can't figure out why....

Here is our process and what is happening:

This is our preliminary query... it gets the group of raw data we will be analyzing (and it works)

SELECT SITEDATA.SITEID
, SITEDATA.ANOMALYT1 + ','
+ SITEDATA.ANOMALYT2 + ','
+ SITEDATA.ANOMALYT3 + ','
+ SITEDATA.ANOMALYT4 + ','
+ SITEDATA.ANOMALYT5 + ','
+ SITEDATA.ANOMALYT6 + ','
+ SITEDATA.ANOMALYT7 + ','
+ SITEDATA.ANOMALYT8 AS ANOMALIES
, SITEDATA.CALC_DATE, SITE.PROPID, SITE.LOCATION
INTO [#TEMP1]
FROM SITEDATA INNER JOIN SITE ON SITEDATA.SITEID = SITE.SITEID
WHERE (SITEDATA.SITEID = 907) AND (SITEDATA.CALC_DATE BETWEEN '1/1/06' AND '12/31/06')

Here is a sample of results from the above Query (Note, there are values in the Anomalies column):

SITEID Anomalies Calc_Date PropID Location
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5
907 ,2,3,,,6,,, 2006-03-26 00:00:00.000 40 5

Now we want to string all the data in the Anomalies column together by Distinct Calc_Date. To do this we execute the following Proc:

SELECT DISTINCT CALC_DATE, SITEID, PROPID, LOCATION, ANOMALIES = SUBSTRING( (
SELECT ',' + ANOMALIES AS [text()] FROM #TEMP1 T1 WHERE T1.SITEID = T2.SITEID FOR XML PATH(''), ELEMENTS ), 3, 1000 )
INTO #TEMP2
FROM #TEMP1 T2

This procedure strings the Anomalies together, but the data appears to be lost, i.e. we should get ,2,3,,,6,,,2,3,6,,,,2,3,,,6, etc. but instead all we get is ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

I thought the issue might be caused by date and time not being distinct, so I tried converting the date to varchar and rerunning, but no difference.... Any thoughts?|||

Jim,

Your query worked perfectly for me. (minor formatting alterations...)


SET NOCOUNT ON

DECLARE @.Temp1 table
( RowID int IDENTITY,
SiteID int,
Anomalies varchar(100),
Calc_Date datetime,
PropID int,
Location int
)

INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )
INSERT INTO @.Temp1 VALUES ( 907, ',2,3,,,6,,,', '2006-03-26 00:00:00.000', 40, 5 )

SELECT DISTINCT
Calc_Date,
SiteID,
PropID,
LOCATION,
Anomalies = substring( ( SELECT ',' + Anomalies AS [text()]
FROM @.Temp1 T2
WHERE T1.SITEID = T2.SITEID
FOR XML PATH(''), ELEMENTS
), 3, 1000
)
INTO #Temp2
FROM @.Temp1 T1

SELECT *
FROM #Temp2

Calc_Date SiteID PropID LOCATION Anomalies

-- -- --
2006-03-26 00:00:00.000 907 40 5 2,3,,,6,,,,,2,3,,,6,,,,,2,3,,,6,,,,,

(Anomalies repetition removed for display purposes...)

|||Thanks again Arnie.

I couldn't replicate your success, however I figured out a different query to and up with the same results. Because the DS is small, I just searched the Anomalies field without combining them, then I looked for distinct date. I think it ended up being faster anyway

Concatination problem

Hi all,

I need to get in my stored procedure name of 'result' field as parameter.
I'm trying to implement that using sp_sqlexec but everytime I get error...
Any ideas?
---------------------------
Create procedure MyProc
@.CurClientID varchar(10)
@.CurCounterName varchar(20)

AS

declare @.ExecString varchar(300)

set @.ExecString = 'Select ' + @.CurCounterName +
' FROM Client WHERE ClientID=' + @.CurClientID

exec sp_sqlexec @.ExecStringOriginally posted by yurich
Hi all,

I need to get in my stored procedure name of 'result' field as parameter.
I'm trying to implement that using sp_sqlexec but everytime I get error...
Any ideas?
---------------------------

this should work :

Create procedure MyProc
@.CurClientID varchar(10)
@.CurCounterName varchar(20)

AS

declare @.ExecString varchar(300)

set @.ExecString = 'Select ' + @.CurCounterName +
' FROM Client WHERE ClientID=' + @.CurClientID

exec sp_sqlexec @.ExecString

Create procedure MyProc
@.CurClientID varchar(10)
@.CurCounterName varchar(20)

AS

declare @.ExecString varchar(300)

set @.ExecString = 'Select ' + @.CurCounterName +
' FROM Client WHERE ClientID=' + @.CurClientID

exec (@.ExecString)|||The holy book[SQL Server Books online] says so :

sp_sqlexec provided a convenient way for SQL Server database clients and servers to send a language statement of any format to an Open Data Services server application. Removed; no longer available. Remove all references to sp_sqlexec.|||It does not help...|||What is the error you are getting ?|||Syntax error converting the varchar value ...|||Originally posted by yurich
Hi all,

I need to get in my stored procedure name of 'result' field as parameter.
I'm trying to implement that using sp_sqlexec but everytime I get error...
Any ideas?
---------------------------
Create procedure MyProc
@.CurClientID varchar(10),-- just add comma !!!!
@.CurCounterName varchar(20)

AS

declare @.ExecString varchar(300)

set @.ExecString = 'Select ' + @.CurCounterName +
' FROM Client WHERE ClientID=' + @.CurClientID

--exec sp_sqlexec @.ExecString
exec(@.ExecString)|||Snail you beat me to it ,

Yurich ...thats the only error i could find in the code .. rest the code seems fine|||I imagine your missing the quotes...

DECLARE @.SQL varvhar(8000)
SELECT @.SQL = 'Select ' + @.CurCounterName
+ ' FROM Client WHERE ClientID=' + '''' + @.CurClientID + ''''
SELECT @.SQL

exec (@.ExecString)|||Originally posted by Brett Kaiser
I imagine your missing the quotes...

DECLARE @.SQL varvhar(8000)
SELECT @.SQL = 'Select ' + @.CurCounterName
+ ' FROM Client WHERE ClientID=' + '''' + @.CurClientID + ''''
SELECT @.SQL

exec (@.ExecString)


It works! Thanks a lot. Now I need to get out result of this query to local variable:

DECLARE @.SQL varvhar(8000)
DECLARE @.Res varchar(100)
SELECT @.SQL = 'Select [@.Res] ' + @.CurCounterName
+ ' FROM Client WHERE ClientID=' + '''' + @.CurClientID + ''''

exec (@.ExecString)
It works, but after executing @.Res = NULL...|||Originally posted by yurich
It works! Thanks a lot. Now I need to get out result of this query to local variable:

DECLARE @.SQL varvhar(8000)
DECLARE @.Res varchar(100)
SELECT @.SQL = 'Select [@.Res] ' + @.CurCounterName
+ ' FROM Client WHERE ClientID=' + '''' + @.CurClientID + ''''

exec (@.ExecString)
It works, but after executing @.Res = NULL...

You cannot save result to local variable but you could save it to temporary table:

create table #tmp(...)
insert #tmp
exec(...)

Concatination of empty field and removal of "" symbols

Hello...

I am going crazy trying to figure out how to do this. I have a flat file which I am massaging the data and loading into a table here is an example of a line out of the flat file:

"ABC NUTRITIONAL PRODUCTS","550","","","N","FAIR OAKS","","","COLORADO SPRINGS","C0","","","","","","","","A","","",""

My problem is that I have one field which is this address in a concatinated form. The fields that do not apply to this entry are suite#, floor# and other columns which are designated by the "" characters. The final concatinated addres field looks like this:

"550""""N""FAIR OAKS"""

I would like to remove the "" characters in the concatinated string. I just don't know the best way to do this? I was told DTS had a way of removing the "" from the flat file source. Since I have not used DTS extensively I am not sure if this is true. I was wondering how in SSIS I could go about removing the "" marks without removing the "" say if someone is quoted eg. John said "This is only a test". Removing the quotation marks in this instance would be changing the data. I am not sure how to do this and any help or advice is greatly appreciated!

Thank you...

SSIS has a powerful expression language that allows you to carry out many operations, including text manipulation.

In your case, you can use this expression language in a Derived Column Transform.

-Jamie

Concatinating two field and insert the result

Hii,
I need to concatinate two field and insert the result into each record. So far I managed to display the concatination but how do I insert it?
use northwind

select city, region,([city]+ +[region]) as uniqe
from customers
where region is not null
The resulting records in Quary
Anchorage AK AnchorageAK
Tsawassen BC TsawassenBC
Vancouver BC VancouverBC
San Francisco CA San FranciscoCA


Try it like this:
UPDATE
Customers
SET
city = city + ' ' + region
WHERE
region IS NOT NULL

concatinating string values from multiple rows

I currently have some SQL code that is used to build a string that is a concatination of string values across multiple rows. The subqueries in the script sometimes return NULL values so I use the following statement to change the default behavior of the concatination operator which prevents my query from returning NULL:

SET CONCAT_NULL_YIELDS_NULL ON

Here's the code snippet:

select DISTINCT

(SELECT CASE WHEN (t1.MaskValue & HDR.TranTypeID)=1 THEN ' ' + t1.description ELSE '' END FROM transactiontypes t1 WHERE (t1.MaskValue & HDR.TranTypeID)=1) +

(SELECT CASE WHEN (t2.MaskValue & HDR.TranTypeID)=2 THEN ' ' + t2.description ELSE '' END FROM transactiontypes t2 WHERE (t2.MaskValue & HDR.TranTypeID)=2) +

(SELECT CASE WHEN (t3.MaskValue & HDR.TranTypeID)=4 THEN ' ' + t3.description ELSE '' END FROM transactiontypes t3 WHERE (t3.MaskValue & HDR.TranTypeID)=4) +

(SELECT CASE WHEN (t4.MaskValue & HDR.TranTypeID)=8 THEN ' ' + t4.description ELSE '' END FROM transactiontypes t4 WHERE (t4.MaskValue & HDR.TranTypeID)=8) +

(SELECT CASE WHEN (t5.MaskValue & HDR.TranTypeID)=16 THEN ' ' + t5.description ELSE '' END FROM transactiontypes t5 WHERE (t5.MaskValue & HDR.TranTypeID)=16)) as 'Transaction Type'

FROM HDResponse HDR

Here's the underlying table structure:

CREATE TABLE [dbo].[TransactionTypes](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Description] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [MaskValue] [int] NOT NULL) ON [PRIMARY]

CREATE TABLE [dbo].[HDResponse](
[ResponseID] [int] IDENTITY(1,1) NOT NULL,
[LoggedDateTime] [datetime] NULL,
[ResponseTypeTripID] [int] NULL,
[ResponseTypeID] [int] NULL,
[ResponseTypeObjectID] [int] NULL,
[ObjectID] [int] NULL,
[IDHolderID] [int] NULL,
[TransportCode] [int] NULL,
[CardID] [int] NULL,
[IssueCode] [smallint] NULL,
[EventDateTime] [datetime] NULL,
[Response] [bit] NULL,
[TranTypeID] [int] NULL)
ON [PRIMARY]

The problem I am having is I need to be able to use the query above in a view used for reporting. Unfortunately, you cannot use SET CONCAT_NULL_YIELDS_NULL ON in a view. This causes my query to return NULL if any of the subqueries return NULL. I could create a function to do something similar and reference the function in the query but I can't help but think there must be a way to get this done in a single query.

Any thoughts or ideas would be greatly appreciated.

Thanks!!!!!

What version of SQL Server are you using? SET CONCAT_NULL_YEILDS_NULL is ON by default unless you have changed the settings after connecting to SQL Server. This is true for any connections made via ODBC/OLEDB. You can use the PIVOT operator in SQL Server 2005 to solve this problem. You can also do this using a standard SQL technique like below:

select max(case p.trantype when 1 then p.description else '' end) +
max(case p.trantype when 2 then ' ' + p.description else '' end) +
max(case p.trantype when 4 then ' ' + p.description else '' end) +
max(case p.trantype when 8 then ' ' + p.description else '' end) +
max(case p.trantype when 16 then ' ' + p.description else '' end) as "Transaction Type"
from (
select t.description, (t.MaskValue & h.TranTypeID) as trantype
from HDResponse as h
join transactiontypes as t
on (t.MaskValue & h.TranTypeID) in (1, 2, 4, 8, 16)
) as p