Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 


SQL wm_concat function

Expert Oracle Tips by Burleson Consulting

September 19, 2010

Question: I have a table test_test and I need to count the distinct mark columns and them display all matching values on one line:

Name    Mark
------- ------
ABC     10
DEF     10
GHI     10
JKL     20
MNO     20
PQR     30


The result should be like this, with the count and the rows groups onto the same line;
 
mark count     names
---- -----     -----------

10       3     ABC,DEF,GHI
20       2     JKL,MNO
30       1     PQR



Answer: By Laurent Schneider:  You could write your own aggregate function or use wm_concat:

select
   mark,
   count(*),
   wm_concat(name)
from
   test_test
group by
   mark;

Here is another example of using wm_concat:

select
   deptno,
   wm_concat(distinct ename)
from
   emp
group by
   deptno;

DEPTNO     WM_CONCAT(DISTINCTENAME)
---------- ----------------------------------------
10         CLARK,KING,MILLER
20         ADAMS,FORD,JONES,SCOTT,SMITH
30         ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD

Another reader asks:

Question:  I have some questions about using the wm_concat function.  I have been told that I can use wm_concat to concatinate some data, but I ran into some issues.  Using wm_concat returned some errors about unexpected data types, so I added a CAST statement to make everything VARCHAR:

cast ( ... as varchar2(...))

Is there a need to use SUBSTR as well?

  • If the result of the wm_concat is 100 chars, can I use the following to truncate the results to 15 characters just like substr(wm_concat(...),1,15)?

    cast(wm_concat(...) as varchar2(15))
  • Assuming the result of the wm_concat is 15 chars and I wanted to CAST it to 4000 chars, would the result be 15 characters or would I get a bunch of white space to go with it?

Answer:  Oracle Guru Laurent Schneider has this to add about the use of CAST and wm_concat:

Regarding the results returned by wm_concat, The use of CAST does indeed resize or expand to the specified length. As for using varchar2 with the wm_concat results, it does not right pad with blank space.  An example of the use of CAST:

 select
   'abcdef',
   cast('abcdef' as varchar2(3)) v3,
   length(cast('abcdef' as varchar2(3)) ) lv3,
   cast('abcdef' as varchar2(9)) v9,
   length(cast('abcdef' as varchar2(9)) ) lv9
from dual;

'ABCDEF' V3         LV3 V9               LV9
-------- --- ---------- --------- ----------
abcdef   abc          3 abcdef             6

Casting to a larger string is more visible in a view:

SQL> create view v as
   2  select cast('abcdef' as varchar2(9)) vc9, substr('abcdef',1,9) sub from dual;

View created.

SQL> desc v

Name              Null?    Type
----------------- -------- ------------
VC9                        VARCHAR2(9)
SUB                        VARCHAR2(6)

Do not use CAST if you need SUBSTR(char), TO_CHAR(number) or TO_CHAR(date) !

It is important to note that wm_concat is an undocumented and unsupported function and should be used wisely.  Its use should be thoroughly tested in a development environment before use in production.

Starting in Oracle 11gR2, LISTAGG is preferred over the unsupported wm_concat function.

Also see:


 
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.

 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright ? 1996 -  2014

All rights reserved by Burleson

Oracle ? is the registered trademark of Oracle Corporation.


 

  
 

 
 
 
 
 

 
 
 
 
 
 
Oracle training Excel
 
Oracle performance tuning software