Data splitting and merging of Oracle tables

Source: Internet
Author: User


Oracle splits and merges table data in daily database operations. Let's take a look at how to implement it.
Assume that the table TEMP_TABLE (ID, NAME, CODE) has the following data: [SQL] select t. * from temp_table t; id name code www.2cto.com 1 A 102 B 203 C 304 A 405 A 506 A | B | C 60
We merge the data with NAME = A and use the Oracle function WM_CONCAT (). The SQL statement and result are as follows: [SQL] select t. name, to_char (wm_concat (t. code) from temp_table t where name = 'A' group by t. name; name codea, 50 because the result returned by WM_CONCAT is of the CLOB type, we can use the TO_CHAR function to convert it to a string, or use the REPLACE function to convert the result ', 'convert to other delimiters we need. Let's take A look at how to split A record into multiple records using the specified delimiter, using ID = 6 in as an example, NAME = A | B | C, we will split it into three records, the SQL statement and result are as follows:
[SQL] SELECT id, REGEXP_SUBSTR (name, '[^ |] +', 1, rn) B FROM temp_table, (SELECT ROWNUM rn FROM DUAL CONNECT BY ROWNUM <= 50) WHERE REGEXP_SUBSTR (name, '[^ |] +', 1, rn) is not null; id name www.2cto.com 1 A2 B3 C4 A5 A6 B6 C
Here, REGEXP_SUBSTR is the sharding function we will use. Split the field NAME with '|' as the separator. We can see the last three lines split the original NAME = A | B | C into three records.

Note that, after the FROM keyword (select rownum rn from dual connect by rownum <= 50), 50 is the maximum number of data records to be split, in this example, 3 is enough.

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.