SQL Server - Adding a string to a text column (concat equivalent)(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 等效项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server - 将字符串添加到文本列(concat 等效项)
基础教程推荐
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
