About SQL Server concatenation strings!

Source: Internet
Author: User

The query results are multiple rows and one column. Now we need to splice these results into a string!

 

Select [values] = stuff (select ',' + coal_id from cbm_well_coalbed_info t for XML Path (''), 1, 1 ,'')

 

It's a bit confusing. I have never touched on such a usage. It's very powerful!

 

1. for XML Path

Convert the query result to XML

A. Select well_id, coal_id from cbm_well_coalbed_info t for XML Path ('liuxm ')

The result is as follows:

<Liuxm>

<Well_id> 8ad882351ede89c6011eecb09b290002 </well_id>

<Coal_id> 8ad882351ec8d47c011ec99e01b6002b </coal_id>

</Liuixm>

 

2. Stuff Function

Deletes a string of the specified length and inserts a group of characters at the specified start point.
Stuff (character_expression, start, length, character_expression)
Character_expression: Operator character,
Start: the start point of deletion and insertion,
Length: delete length,
Character_expression: the character to be inserted.

 

Record it first, and then study again...

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.