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)
 merge fields

Author  Topic 

Exir
Posting Yak Master

151 Posts

Posted - 2008-12-30 : 05:00:40
Is it possible to merge 3 fields of a table and insert them in the other field of same table?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 05:03:54
yup. just use

UPDATE Table
SET DestyField=Field1+Field2 +Field3

I assume all three fields field1,field2,... etc are of type varchar else you might need to cast or convert before merging.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 05:05:46
also if any of columns can contain NULLs, make sure you convert them to blank before merging, else result will be NULL provided your CONCAT NULL YIELDS NULL setting is on

UPDATE Table
SET DestField=COALESCE(Field1,'')+COALESCE(Field2,'') +COALESCE(Field3,'')
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2008-12-30 : 06:01:51
my data types are int and when i use plus sign, all fields add together and he result insert in the destination field. how should i convert them to nvarchar?
i tryed two methods bellow bot none of them worked
destfield=(CAST @field1 AS nvarchar(50)+CAST @field2 AS nvarchar(50)+CAST @field3 AS nvarchar(50))
destfield= (convert(@field1,nvarchar(50))+convert(@field2,nvarchar(50))
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 06:15:27
you missed some brackets

UPDATE Table
SET DestField=COALESCE(CAST(Field1 AS varchar(50)),'')+COALESCE(CAST(Field2 AS varchar(50)),'') +COALESCE(CAST(Field3 AS varchar(50)),'')
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2008-12-30 : 07:15:20
it worked , thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 11:41:53
cheers
Go to Top of Page
   

- Advertisement -