开发者

Updating a column with concatenated value

开发者 https://www.devze.com 2023-01-20 22:09 出处:网络
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 ty开发者_如何学Pythonpe nvarchar;

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 ty开发者_如何学Pythonpe nvarchar;

Is this possible?


Yes, of course:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消