Implementation of comma-separated column-to-row in Oracle

Source: Internet
Author: User

Now, if a permission number is provided and you want to retrieve the user set with the permission, you need to match the given permission number among multiple permission numbers separated by commas. If you use like, one is less efficient, and the other is inaccurate. Therefore, the comma-separated column-to-row method is used. Currently, this method is only applicable to oracle databases. This method only requires SQL statements to implement column-to-row conversion.

The following is an example of this method:

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 <= 100) c where c. lv <= t. cnt)

Run the preceding code. The result is as follows:

The content "1, 2, 3" in the simulation data column c is converted into three rows, and the content of the first two columns is included in the past. In actual use, you only need

Select 'aaa' a, 'bbb 'B, '1, 2, 3' c from dual to replace the fields in the data table to be converted, the c field must be the field for saving the comma-separated content for conversion. The following content does not need to be changed. The number 100 in "LEVEL <= 100" indicates the number of times that a comma appears in the content of the matching field. You can change the value on your own.

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.