Converting SQL column data to strings

Source: Internet
Author: User

Row and column conversions, converting columns data to string output

DECLARE @center_JZHW VARCHAR( -)SET @center_JZHW =(SELECT DISTINCT STUFF(    ( SELECT  ',' +Ce_code fromAp_center with(NOLOCK)WHERECe_province= 'Zhejiang province' ORDER  byCe_code forXML PATH ("')),1,1,"') asJZHW fromap_center t)SELECT @center_JZHW
View Code

Run results such as, column ce_code with delimiter ', ' output

function notation

DECLARE @ret VARCHAR( $)        SET @ret = "'        SELECT  @ret = @ret + '|' + RTRIM(Rt_gsx_comptia_code) fromdbo. Ap_request_return_partWHERERt_so_no= @so_no        SET @ret =  Case  when LEN(@ret)> 0  Then STUFF(@ret,1,1,"')                        ELSE @ret                   END        RETURN @ret 
View Code


The execution output is

Multiple columns as field output:

SELECT DISTINCT STUFF(    ( SELECT  ',' +Rt_loan_part fromDbo. Ap_request_return_partWHERERt_so_no='IPTH14060001'       forXML PATH ("')),1,1,"') asPart ,STUFF(    ( SELECT  ',' +Rt_gsx_comptia_code fromDbo. Ap_request_return_partWHERERt_so_no='IPTH14060001'       forXML PATH ("')),1,1,"') asComptia_code fromAp_request_return_part T
View Code

Execution Result:

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.