2 SQL functions Dbms_lob.substr and Wm_concat

Source: Internet
Author: User

Turn from:

http://blog.csdn.net/wenzhongyan/article/details/50315473

http://blog.csdn.net/ojerryzuo/article/details/53927057

1. Convert clob field to VARCHAR2 type by DBMS_LOB.SUBSTR ()

When querying the Dba_stat_extensions view, where the extension field is the CLOB type, it cannot be displayed directly through the SELECT statement, as follows:

Need to convert by DBMS_LOB.SUBSTR ()

SELECT owner, A.table_name,trim (Dbms_lob.substr (extension,4000)) as extension from Dba_stat_extensions A is displayed as follows:

2.

First let's take a look at this magical function wm_concat (column name), which separates the column values with "," and displays them in a row, and then the example below to see how this magical function applies to prepare test data sql> CREATE TABLE test (ID Number,name varchar2 (20)); sql> INSERT INTO test values (1, ' a '); sql> INSERT INTO test values (1, ' B '); sql> INSERT INTO test values (1, ' C '); sql> INSERT INTO test values (2, ' d '); sql> INSERT INTO test values (2, ' e '); Sql> commit; effect 1: Row to column sql> select Wm_concat (name) from test; Wm_concat (NAME)-------------------------------------------------------------------------A,b,c,d,e effect 2: Replace the comma in the result with the "|" Sql> Select Replace (wm_concat (name), ', ', ' | ') from test; REPLACE (Wm_concat (NAME), ', ', ' | ') -----------------------------------------------------------------------A|b|c|d|e Effect 3: Group by ID merge namesql> Select ID , Wm_concat (name) name from test Group by id;id name----------------------------------------1 a,b,c2 d,e Lazy Extension usage: case: I'm going to write a view like "Create or Replace view as Select field 1,... field from TableName ", the base table has more than 50 fields, if it is too cumbersome to write by hand, is there any easy way? Sure, look at me. If you apply wm_concat to make this requirement simple sql> Select ' Create or Replace viewAs SELECT ' | | Wm_concat (column_name) | | ' From dept ' from User_tab_columns where table_name= ' dept '; ' Createorreplaceviewasselect ' | | Wm_concat (column_name) | | ' Fromdept '--------------------------------------------------------------------------------Create or replace view as Select Deptno,dname,loc from Dept

2 SQL functions Dbms_lob.substr and Wm_concat

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.