2

我正在使用 SQL Server 2008 R2 并尝试运行以下命令:

SELECT CustCity AS City, Concat(CustLastName, \', \', CustFirstName) AS Customer
FROM Customers
ORDER BY CustCity, Customer

我收到的错误消息是:

消息 102,级别 15,状态 1,第 1 行
',\' 附近的语法不正确。

据我所知,这是Concat应该如何工作的,所以我不明白为什么这不起作用。

任何人都可以帮忙吗?

4

4 回答 4

2

你可以这样尝试:-

SELECT CustCity AS City, CustLastName + '\' + CustFirstName AS Customer
FROM Customers
ORDER BY CustCity, Customer

SQL SERVER 2008 中不存在 CONCAT 函数。

于 2013-09-06T15:52:53.140 回答
1

改为这样做:

SELECT CustCity AS City, CustLastName + '\' + CustFirstName AS Customer
FROM Customers
ORDER BY CustCity, Customer
于 2013-09-06T15:52:28.893 回答
1

CONCAT()不是 SQL Server 2008 的功能。如果要连接 sql server 中的数据,应使用+符号

SELECT CustCity AS City, CustLastName + ', ' + CustFirstName AS Customer
FROM Customers
ORDER BY CustCity, Customer;

请参阅SQL Fiddle with Demo。如果要包含\,则可以使用:

SELECT CustCity AS City, CustLastName + ' \ ' + CustFirstName AS Customer
FROM Customers
ORDER BY CustCity, Customer

演示

于 2013-09-06T15:52:53.707 回答
0

相当肯定您的单引号'引起了问题。问题是 CONCAT 仅存在于 SQL 2012 而不是 2008 中。

如果您使用的是 SQL 2012,则正确的语法是。

Concat(CustLastName, '\', CustFirstName) AS Customer
于 2013-09-06T15:52:52.517 回答