如何将字符串添加到 SQL Server 中的列?
UPDATE [myTable] SET [myText]=' '+[myText]
那是行不通的:
数据类型 varchar 和 text 在 add 运算符中不兼容。
您会在 MySQL 上使用 concat,但在 SQL Server 上如何执行呢?
就像之前所说,最好是将列的数据类型设置为 nvarchar(max),但如果不可能,您可以使用强制转换或转换执行以下操作:
-- create a test table
create table test (
a text
)
-- insert test value
insert into test (a) values ('this is a text')
-- the following does not work !!!
update test set a = a + ' and a new text added'
-- but this way it works:
update test set a = cast ( a as nvarchar(max)) + cast (' and a new text added' as nvarchar(max) )
-- test result
select * from test
-- column a contains:
this is a text and a new text added
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)