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.
| 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 useUPDATE TableSET 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. |
 |
|
|
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 onUPDATE TableSET DestField=COALESCE(Field1,'')+COALESCE(Field2,'') +COALESCE(Field3,'') |
 |
|
|
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 workeddestfield=(CAST @field1 AS nvarchar(50)+CAST @field2 AS nvarchar(50)+CAST @field3 AS nvarchar(50))destfield= (convert(@field1,nvarchar(50))+convert(@field2,nvarchar(50)) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 06:15:27
|
you missed some bracketsUPDATE TableSET DestField=COALESCE(CAST(Field1 AS varchar(50)),'')+COALESCE(CAST(Field2 AS varchar(50)),'') +COALESCE(CAST(Field3 AS varchar(50)),'') |
 |
|
|
Exir
Posting Yak Master
151 Posts |
Posted - 2008-12-30 : 07:15:20
|
| it worked , thank you. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 11:41:53
|
| cheers |
 |
|
|
|
|
|
|
|