SQL Server converts multiple rows in one column into strings using the for XML Path.

Source: Internet
Author: User
SQL Code
  1. Create TableTB ([ID]Int,[Name]Varchar(2 ))
  2. Insert IntoTB
  3. Select1,'A' Union All
  4. Select2,'Bb' Union All
  5. Select1,'Cc' Union All
  6. Select3,'Dd' Union All
  7. Select2,'Ee'
  8. Select*FromTB
  9. -- Query
  10. Select
  11. ID,
  12. Name= Stuff ((Select ','+Name FromTBWhereId = T. IDForXML Path (''), 1, 1,'')
  13. From
  14. TB t
  15. Group By
  16. ID
  17. Method 2:
  18. Create FunctionGet_string (@ IDInt)
  19. Returns Varchar(50)
  20. As
  21. Begin
  22. Declare@Name Varchar(500)
  23. Select@Name=Isnull(@Name+',','') +Name FromTBWhereId = @ ID
  24. Return@Name
  25. End
  26. SelectID, DBO. get_string (ID)Name FromTBGroup ByID
  27. -----------------------------------------------------------------------
  28. In Oracle, there is a corresponding function, the sys_connect_by_path function.
  29. Let's write down the Oracle function implementation here first.
  30. SelectID
  31. , Itrim (Max(Sys_connect_by_path (Name,',')))As Name
  32. From
  33. (SelectID
  34. ,Name
  35. , Lead (rnfirst) over (partitionBy No Order ByRnfirst) rnnext
  36. From
  37. (SelectA. ID
  38. ,.Name
  39. , Row_number () over (Order ByA. ID,.Name Desc) Rnfirst
  40. From@ T a) tmptable1
  41. ) Tmptable2
  42. StartWithRnnextIs Null
  43. Connect ByRnnext =PriorRnfirst
  44. Group ByID
  45. The preceding method is used to convert rows to columns in Oracle.
  46. **************************************** * *** Let's take a look at sqlserver, because sqlserver does not provide similar functions, I do not know whether sqlserver2012 provides
  47. However, SQL Server is easy to support.CrossApply andForThe XML Path combination is used, and both of them know what is going on and how to use it. But I really don't know whether there are two combinations to complete this function. It's incredible, I would like to thank the people on the Internet who wrote everything on their own to record the learned knowledge to their local computer and never put it on the Internet. Maybe it's the latest. I don't know a lot of work, all through the help of the network, we can put everything we know on the Internet and learn together.
  48. Sqlserver:
  49. SelectID,Substring(Names, 1, Len (names)-1)From@ TAsACrossApply
  50. (Select Name+',' From@ TAsBWhereA. ID = B. IDForXML Path ('') D (names)
  51. Group ByID, names

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.