views:

30

answers:

2

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?

+3  A: 

Yes, of course:

UPDATE dbo.YourTable
SET NVarcharField = CAST(id AS NVARCHAR(10)) + CAST(VarCharField AS NVARCHAR(50))
WHERE (some condition)
marc_s
Is there ever any need in explicit cast/conversion betweeen NVarChar and VarChar type values and when, I wonder?
vgv8
A: 

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;
vgv8