Convert a string to a column display

Source: Internet
Author: User

--1. Converting a string to a column displays if OBJECT_ID (' TB ') is not null drop table TB go CREATE TABLE TB ([number] varchar (3), [Product] varchar (2), [qty] int, [Unit Price] int,[amount] int,[serial number] varchar (8)) insert into TB ([number],[product],[quantity],[Unit price],[amount],[Serial number]) SELECT ' 001 ', ' AA ', 3,5,15, ' 12,13,14 ' Union Allselect ' 002 ', ' BB ', 8,9,13, ' 22,23,24 ' Go SELECT [number],[product],[quantity],[Unit price],[amount], substring ([Serial number],b.number,charindex ( ', ', [serial number]+ ', ', B.number]-b.number) as [serial number] from TB A with (NOLOCK), Master: Spt_values B with (NOLOCK) where B.number>=1 and B.number<len (a.[serial number]) and b.type= ' P ' and substring (', ' +[serial number], number,1) = ', ' Go drop table TB go/** number Product quantity Unit price amount serial number-------------------------------  ------------------001 AA 3 5 001 AA 3 5 15 13 001           AA 3 5 002 BB 8 9 002 BB 8 9  002 BB 8 9 */----------

Convert a string to a column display

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.