SQL select语句字符串连接 [英] SQL select statement string concatenation

查看:259
本文介绍了SQL select语句字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用select语句可以这样做:

  SELECT col1,concat(col2 +'')FROM。 ... 
GROUP BY col1

我知道我可以使用count(col2)或sum (col2)为整数,但是有一个函数用于连接,如果类型是nvarchar或nchar?

解决方案

在SQL Server中,如果你想跨行连接,没有内置的函数来做到这一点。

我个人喜欢使用XML PATH ,因为它似乎表现良好,但是这只能在SQL Server 2005以后才能使用

  SELECT 
STUFF(

SELECT
''+)描述
FROM dbo.Brands
FOR XML PATH('')
), 1,1,''
)as concatenated_string


Can something like this be done with a select statement:

SELECT col1, concat(col2 + ' ') FROM ....
       GROUP BY col1

I know i can use count(col2) or sum(col2) for integers but is there a function for concatenating if the type is nvarchar or nchar?

解决方案

In SQL Server, if you want to concatenate across rows, there is no built in function to do this.

I personally like using XML PATH as it seems to perform well, but this will work only in SQL Server 2005 onwards

SELECT
  STUFF(
    (
    SELECT
      ' ' + Description
    FROM dbo.Brands
    FOR XML PATH('')
    ), 1, 1, ''
  ) As concatenated_string

这篇关于SQL select语句字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆