Oracle String concatenation

Source: Internet
Author: User

-- Three methods can be implemented:
-- Method 1: The version of wmsys. wm_concat Oracle must be 10 Gb or above.
-- Method 2: Use sys_connect_by_path for the Oracle version 9i and below
-- Method 3: Use User-Defined Functions

-- I will give you an example of UDF implementation.
-- View the employee name with Department 10 in the employee table

-- UDF implementation

Create or replace function my_concat (p_deptno in integer) return varchar2 -- remember: The parameter and the data type in the return value do not need to define the length is result varchar2 (4000); -- Define the variable, remember that the definition of variables in Oracle does not require begin for temp_cursor in (select ename from EMP where deptno = p_deptno) loop -- in this case, all employees in the corresponding department are queried in the cursor for loop: = Result | temp_cursor.ename | ','; end loop; Result: = rtrim (result, ','); -- remove the last space, return result; end;

 
Select distinct deptno, my_concat (deptno) name from EMP where deptno = 10;

Result:
Deptno name
-----------------------
30 Allen, Ward, Martin, Blake, Turner, James, black, Maxton

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.