我正在寻找CONCAT
SQL Server 2008 R2 中的函数。我找到了这个函数的链接。但是当我使用这个函数时,它给出了以下错误:
消息 195,级别 15,状态 10,第 7 行
“CONCAT”不是可识别的内置函数名称。
CONCAT
SQL Server 2008 R2 中是否存在该函数?
如果没有,如何在 SQL Server 2008 R2 中连接字符串?
我正在寻找CONCAT
SQL Server 2008 R2 中的函数。我找到了这个函数的链接。但是当我使用这个函数时,它给出了以下错误:
消息 195,级别 15,状态 10,第 7 行
“CONCAT”不是可识别的内置函数名称。
CONCAT
SQL Server 2008 R2 中是否存在该函数?
如果没有,如何在 SQL Server 2008 R2 中连接字符串?
CONCAT
是 SQL Server 2012 的新功能。您提供的链接说明了这一点,它不是以前版本的功能,包括 2008 R2。
在文档树中可以看到它是 SQL Server 2012 的一部分:
SQL Server 2012
Product Documentation
Books Online for SQL Server 2012
Database Engine
Transact-SQL Reference (Database Engine)
Built-in Functions (Transact-SQL)
String Functions (Transact-SQL)
编辑Martin Smith 有帮助地指出SQL Server 提供了 ODBCCONCAT
函数的实现。
I suggest you cast all columns before you concat them
cast('data1' as varchar) + cast('data2' as varchar) + cast('data3' as varchar)
This should work for you.
如前所述,CONCAT 在 SQL Server 2012 之前不受支持。但是,您可以按照建议使用 + 运算符进行连接。但请注意,如果第一个操作数是数字,则此运算符将引发错误,因为它认为将添加而不是连接。要解决此问题,只需在前面添加“”。例如
someNumber + 'someString' + .... + lastVariableToConcatenate
会引发错误但'' + someNumber + 'someString' + ......
会正常工作。
Also, if there are two numbers to be concatenated make sure you add a '' between them, like so
.... + someNumber + '' + someOtherNumber + .....
NULL safe drop in replacement approximations for SQL Server 2012 CONCAT function
SQL Server 2012:
SELECT CONCAT(data1, data2)
PRE SQL 2012 (Two Solutions):
SELECT {fn CONCAT(ISNULL(data1, ''), ISNULL(data2, ''))}
SELECT ISNULL(CAST(data1 AS varchar(MAX)), '') + ISNULL(CAST(data2 AS varchar(MAX)), '')
These two solutions collate several excellent answers and caveats raised by other posters including @Martin Smith, @Svish and @vasin1987.
These options add NULL
to ''
(empty string) casting for safe NULL
handling while accounting for the varying behaviour of the +
operator pertaining to specific operands.
Note the ODBC Scaler Function solution is limited to 2 arguments whereas the +
operator approach is scalable to many arguments as needed.
Note also the potential issue identified by @Swifty regarding the default varchar
size here remedied by varchar(MAX)
.
(city + ', ' + state + ' ' + zip) as ctstzip for select
(city + ', ' + state + ' ' + zip) for insert
Only cast or convert if any field type is different from others.
On insert the value needs to be in the correct spot you need it be inserted. Using "as" will give you an error.
i.e.
Insert into testtable (ctstzip) Values ((city + ', ' + state + ' ' + zip))
Yes the function is not in sql 2008. You can use the cast operation to do that.
For example we have employee
table and you want name
with applydate
.
so you can use
Select cast(name as varchar) + cast(applydate as varchar) from employee
It will work where concat function is not working.
You can use '+' between the strings that you want to concat like
SELECT string1 + string2
If one of those give conversion error like if one of the columns is an int column you should cast it before concatenating the columns like
SELECT (CONVERT(nvarchar, intColumn) + string2