109

我正在寻找CONCATSQL Server 2008 R2 中的函数。我找到了这个函数的链接。但是当我使用这个函数时,它给出了以下错误:

消息 195,级别 15,状态 10,第 7 行
“CONCAT”不是可识别的内置函数名称。

CONCATSQL Server 2008 R2 中是否存在该函数?

如果没有,如何在 SQL Server 2008 R2 中连接字符串?

4

8 回答 8

106

仅出于完整性考虑 - 在 SQL 2008 中,您将使用加号+运算符来执行字符串连接。

查看带有示例代码的MSDN 参考。从 SQL 2012 开始,您可能希望使用新的CONCAT 函数

于 2012-06-17T19:23:52.187 回答
70

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函数的实现。

于 2012-05-11T11:19:24.813 回答
50

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.

于 2014-02-11T13:08:36.260 回答
23

如前所述,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 + .....
于 2013-04-08T04:29:40.743 回答
8

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).

于 2017-11-21T21:48:00.100 回答
3
(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))
于 2016-08-25T17:50:28.360 回答
2

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.

于 2019-09-11T09:05:03.623 回答
0

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
于 2021-08-04T09:25:25.420 回答