Oracle comma-delimited column switch implementation method _oracle

Source: Internet
Author: User
Now, if you give a permission number to retrieve a collection of users with this permission, you will need to match the given permission number in a comma-delimited number of permission numbers. If you use like to do, an inefficient, two matching is not accurate. So we used a comma-delimited list of ways to change careers. Currently, this method is only suitable for use in Oracle databases. This method requires only SQL statements to implement column careers.

An example of this method is given below:

Select A,b,c from
(with test as (select ' AAA ' A, ' BBB ' B, ' 1,2,3 ' C from dual)
Select A,b,substr (T.ca,instr (t.ca, ', ', 1, c.lv) + 1,instr (t.ca, ', ', 1, c.lv + 1)-(InStr (t.ca, ', ', 1, c.lv) + 1)) as C
From (select A,b, ', ' | | c | | ', ' as Ca,length (c | | ', ')-NVL (Length (REPLACE (c, ', '), 0) as CNT from test) T,
(Select level LV from dual CONNECT by level <=) C where c.lv <= t.cnt)

Execute the above code and the results are as follows:

The content "1,2,3" in the Analog data column C is turned into three lines, with the contents of the preceding two columns being accompanied by the past. In practical use, as long as the

Select ' AAA ' A, ' BBB ' B, ' 1,2,3 ' C from dual replaced with a field in the datasheet that actually needs to be converted, where the C field must be a field that preserves the comma-delimited content to be converted. None of the following can be changed. The number 100 in Level <= 100 represents the number of times a comma appears in the matching field's contents and can be changed on its own.

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.