SQL无法连接

时间:2013-09-06 15:49:09

标签: sql sql-server string sql-server-2008-r2 concatenation

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

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

我收到的错误消息是:

  

Msg 102,Level 15,State 1,Line 1
  ',''附近的语法不正确。

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

有人可以帮忙吗?

4 个答案:

答案 0 :(得分:2)

您可以尝试这样: -

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

SQL SERVER 2008中不存在CONCAT功能。

答案 1 :(得分:1)

请改为:

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

答案 2 :(得分:1)

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

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

请参阅Demo

答案 3 :(得分:0)

相当确定您的单引号'是导致问题的原因。问题是CONCAT仅存在于SQL 2012而不是2008中。

如果您使用的是SQL 2012,那么正确的语法就是。

Concat(CustLastName, '\', CustFirstName) AS Customer