Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 String concatenation

Author  Topic 

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2011-01-30 : 22:05:48
Dear All,

I have table where i need to do the column and row concatenation for some of the fields.
Table structure:
Id address1 address2 address3
1 A B C
2 X Y Z
My output should be in the form of

ID changes
1 address1=A address2=B address3=C
2 address1=X address2=Y address3=Z
How to do this pls help me.


Thanks,
Gangadhara MS
SQL Developer and DBA

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-30 : 22:33:30
You should not be doing this in SQL. Your output is just adding the column names in there, which would require dynamic SQL if you were to do this in T-SQL. That's not good. This format can easily be achieved in your application, so please do it there instead of in T-SQL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2011-01-30 : 22:53:09
Thanks Tara.

As this is for the data extraction i need to do it in T-SQL only.Don't have any other front end application to process the data.

This output format is required to pull the data in database only.



Thanks,
Gangadhara MS
SQL Developer and DBA
Go to Top of Page

sathishmangunuri
Starting Member

32 Posts

Posted - 2011-01-31 : 00:02:37
Hi,
As per my understand i have written this.Is this work for you?

create table #t1(id int, address1 varchar(10),address2 varchar(10),address3 varchar(10))
insert into #t1 values(1,'x','y','z')
insert into #t1 values(2,'a','b','c')

select id,'address1='+address1,'address2='+address2,'address3='+address3 from #t1

sathish
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-31 : 00:05:40
check this out!

Select
ID, 'Address1='+Address1+' address2='+Address2 +' Address3='+Address3 As Changes
From TableName

Go to Top of Page
   

- Advertisement -