punit
punit

Reputation: 11

Updating a column with concatenated value

I have an id field (int type) and varchar field.

I have to concatenate both columns and store the result to another column with data type nvarchar;

Is this possible?

Upvotes: 1

Views: 1368

Answers (2)

You can create your new NVARCHAR column as computed one.

CREATE TABLE TestInsertComputedColumn  
( 
    ID int, 
    Name VARCHAR(50) 
);   

insert into TestInsertComputedColumn(ID,Name) 
     select 8, 'vgv'; 
select * from TestInsertComputedColumn; 

ALTER TABLE TestInsertComputedColumn  
      ADD FullName As Name + cast(id as nvarchar); 

select * from TestInsertComputedColumn; 
--drop TABLE TestInsertComputedColumn;

Upvotes: 0

marc_s
marc_s

Reputation: 754488

Yes, of course:

UPDATE dbo.YourTable
SET NVarcharField = CAST(id AS NVARCHAR(10)) + CAST(VarCharField AS NVARCHAR(50))
WHERE (some condition)

Upvotes: 4

Related Questions