本文介紹了SQL Server - 將字符串添加到文本列(concat 等效項)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何向 SQL Server 中的列添加字符串?
How do you add a string to a column in SQL Server?
UPDATE [myTable] SET [myText]=' '+[myText]
這不起作用:
數據類型 varchar 和 text 在加法運算符中不兼容.
The data types varchar and text are incompatible in the add operator.
你會在 MySQL 上使用 concat,但你如何在 SQL Server 上使用它?
You would use concat on MySQL, but how do you do it on SQL Server?
推薦答案
就像之前所說的,最好將列的數據類型設置為 nvarchar(max),但如果這不可能,您可以使用 cast 或 convert 執行以下操作:
like said before best would be to set datatype of the column to nvarchar(max), but if that's not possible you can do the following using cast or convert:
-- 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
希望有幫助
這篇關于SQL Server - 將字符串添加到文本列(concat 等效項)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!