doc-exports/docs/dws/tool/dws_mt_0137.html
Lu, Huayi 346ac31da9 DWS TG 8.1.3.200 VERSION
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Reviewed-by: Jiang, Beibei <beibei.jiang@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2023-08-28 09:20:17 +00:00

121 lines
17 KiB
HTML

<a name="EN-US_TOPIC_0000001188521064"></a><a name="EN-US_TOPIC_0000001188521064"></a>
<h1 class="topictitle1">String Functions</h1>
<div id="body8662426"><p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p1314323315110">This section describes the following string functions:</p>
<ul id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_ul14646237318"><li id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_li764613373111"><a href="#EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section19572182825420">LISTAGG</a></li><li id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_li14921194220117"><a href="#EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section1645916521546">STRAGG</a></li><li id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_li136842047113"><a href="#EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section266693805512">WM_CONCAT</a></li><li id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_li62821952612"><a href="#EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section11995195695518">NVL2 and REPLACE</a></li><li id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_li2917114724"><a href="#EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section866063713564">QUOTE</a></li></ul>
<div class="section" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section19572182825420"><a name="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section19572182825420"></a><a name="en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section19572182825420"></a><h4 class="sectiontitle">LISTAGG</h4><p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p1349391614404"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b42351215163759">LISTAGG</strong> is used to order data in columns within each group specified in the <strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b93691969578">ORDER BY</strong> clause and concatenates the order results.</p>
<div class="fignone" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_fig42932995202534"><span class="figcap"><b>Figure 1 </b>Input - Listagg</span><br><span><img id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_image55020556202534" src="figure/en-us_image_0000001188681114.png"></span></div>
<div class="fignone" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_fig27480050202534"><span class="figcap"><b>Figure 2 </b>Output - Listagg</span><br><span><img id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_image11291548202534" src="figure/en-us_image_0000001188362642.png"></span></div>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p286914517540">LISTAGG can be migrated after <strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b165948914193">MigSupportForListAgg </strong>is set to <strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b75941692196">false</strong>.</p>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p7870051195415"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b20996175915317">Input- LISTAGG</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen8870751155412">SELECT LISTAGG(BRANCH_ID, ',') WITHIN GROUP(ORDER BY AREA_ORDER) PRODUCTRANGE
FROM (SELECT DISTINCT VB.BRANCH_ID,
VB.VER_ID,
VB.AREA_ORDER
FROM SPMS_VERSION_BRANCH VB, SPMS_NODE_SET NS
WHERE VB.BRANCH_TYPE IN ('1', '3')
AND VB.AGENCY_BRANCH = NS.BRANCH_ID);</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p14870051135412"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b7997759115317">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen5870751125419">SELECT LISTAGG (BRANCH_ID,',') WITHIN GROUP (
ORDER BY AREA_ORDER ) PRODUCTRANGE
FROM ( SELECT
DISTINCT VB.BRANCH_ID
,VB.VER_ID
,VB.AREA_ORDER
FROM
SPMS_VERSION_BRANCH VB
,SPMS_NODE_SET NS
WHERE VB.BRANCH_TYPE IN (
'1','3')
AND VB.AGENCY_BRANCH = NS.BRANCH_ID)
;</pre>
</div>
<div class="section" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section1645916521546"><a name="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section1645916521546"></a><a name="en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section1645916521546"></a><h4 class="sectiontitle">STRAGG</h4><p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p44023661222650"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b1026774012710">STRAGG</strong> is a string aggregate function used to collect values from multiple rows into a comma-separated string.</p>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p117110112117"><strong id="EN-US_TOPIC_0000001188521064__b1315203317208"><strong id="EN-US_TOPIC_0000001188521064__b1315183342015">Input-</strong>STRAGG</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen1471408213">SELECT DEPTNO,ENAME,STRAGG(ename) over (partition by deptno order by
ename RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
AS ENAME_STR FROM EMP;</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p771707214"><strong id="EN-US_TOPIC_0000001188521064__b1591484117">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen4715022110">SELECT DEPTNO,ENAME,STRING_AGG (
ename,',') over( partition BY deptno ORDER BY
ename RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING ) AS ENAME_STR
FROM EMP
;</pre>
</div>
<div class="section" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section266693805512"><a name="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section266693805512"></a><a name="en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section266693805512"></a><h4 class="sectiontitle">WM_CONCAT</h4><p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p26788861222559"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b64913096163754">WM_CONCAT</strong> is used to aggregate data from a number of rows into a single row, giving a list of data associated with a specific value.</p>
<div class="fignone" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_fig26866048202739"><span class="figcap"><b>Figure 3 </b>Input - WM_Concat</span><br><span><img id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_image28666250202739" src="figure/en-us_image_0000001234200719.png"></span></div>
<div class="fignone" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_fig40264896202739"><span class="figcap"><b>Figure 4 </b>Output - WM_Concat</span><br><span><img id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_image40231180202739" src="figure/en-us_image_0000001234200717.png"></span></div>
</div>
<div class="section" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section11995195695518"><a name="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section11995195695518"></a><a name="en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section11995195695518"></a><h4 class="sectiontitle">NVL2 and REPLACE</h4><p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p5453750614596"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b1272151205212">NVL2</strong>( expression, value1, value2) is a function used to determine the value returned by a query based on whether a specified expression is null or not. If the expression is not null, then NVL2 returns value1. If the expression is null, then NVL2 returns value2.</p>
</div>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p568125110123"><strong id="EN-US_TOPIC_0000001188521064__b15445408212">Input - <strong id="EN-US_TOPIC_0000001188521064__b184459092118">NVL2</strong></strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen197065112126">NVL2(Expr1, Expr2, Expr3)</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p872105191215"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b115307271387">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen197605161218">DECODE(Expr1, NULL, Expr3, Expr2)</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p1879951161219">The <strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b8198526162011">REPLACE </strong>function is used to return char with every occurrence of search_string replaced with replacement_string. If replacement_string is omitted or null, then all occurrences of search_string are removed.</p>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p579851121214">The <strong id="EN-US_TOPIC_0000001188521064__b168714715469">REPLACE</strong> function in Oracle contains two mandatory parameters and one optional parameter. The <strong id="EN-US_TOPIC_0000001188521064__b10206187194710">REPLACE</strong> function in <span id="EN-US_TOPIC_0000001188521064__text1958020141713">GaussDB(DWS)</span> contains three mandatory parameters.</p>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p883251181212"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b111191559111014">Input - Nested <strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b101192596108">REPLACE</strong></strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen6119115961010">CREATE
OR REPLACE FUNCTION F_REPLACE_COMMA ( IS_STR IN VARCHAR2 ) RETURN VARCHAR2 IS BEGIN
IF
IS_STR IS NULL
THEN RETURN NULL ;
ELSE
RETURN REPLACE( REPLACE( IS_STR ,'a' ) ,CHR ( 10 ) ) ;
END IF ;
END F_REPLACE_COMMA ;
/</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p812235971014"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b13122175911103">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen16122185918108">CREATE
OR REPLACE FUNCTION F_REPLACE_COMMA ( IS_STR IN VARCHAR2 ) RETURN VARCHAR2 IS BEGIN
IF
IS_STR IS NULL
THEN RETURN NULL ;
ELSE
RETURN REPLACE( REPLACE( IS_STR ,'a' ,'' ) ,CHR ( 10 ) ,'' ) ;
END IF ;
end ;
/</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p191823519129"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b19763132114140">Input - More than one <strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b476311216141">REPLACE</strong></strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen618915518128">SELECT
REPLACE( 'JACK and JUE' ,'J', '' ) "Changes"
,REPLACE( 'JACK1 and JUE' ,'J' ) "Changes1"
,REPLACE( 'JACK2 and JUE' ,'J' ) "Changes2"
FROM
DUAL
;</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p13199145151215"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b1078342118145">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen15202551191216">SELECT
REPLACE( 'JACK and JUE' ,'J' ,'' ) "Changes"
,REPLACE( 'JACK1 and JUE' ,'J' ,'' ) "Changes1"
,REPLACE( 'JACK2 and JUE' ,'J' ,'' ) "Changes2"
FROM
DUAL
;</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p1521235161215"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b5683131771613">Input - <strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b1768421716162">REPLACE</strong></strong><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b340893416162"> with Three parameters</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen821710517120">SELECT
REPLACE( '123tech123' ,'123', '1')
FROM
dual
;</pre>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p169420001537"><strong id="EN-US_TOPIC_0000001188521064__b967460030">Output</strong></p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen301247461537">SELECT
REPLACE( '123tech123' ,'123' , '1' )
FROM
dual
;</pre>
<div class="section" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section866063713564"><a name="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section866063713564"></a><a name="en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_section866063713564"></a><h4 class="sectiontitle">QUOTE</h4><p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p18964659221627"><strong id="EN-US_TOPIC_0000001188521064__en-us_topic_0237712489_b60034685174416">QUOTE</strong> allows the user to embed single-quotes in literal strings without having to resort to double quotes. That is, you can use single quotes to specify a literal string.</p>
<p id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_p103801939135812">For example:</p>
<pre class="screen" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_screen18442738174435">SELECT q'[I'm using quote operator in SQL statement]' "Quote (q) Operator" FROM dual;</pre>
<div class="fignone" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_fig38044913203010"><span class="figcap"><b>Figure 5 </b>Input - Quote</span><br><span><img id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_image61739150203010" src="figure/en-us_image_0000001188362646.png"></span></div>
<div class="fignone" id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_fig34815243203010"><span class="figcap"><b>Figure 6 </b>Output - Quote</span><br><span><img id="EN-US_TOPIC_0000001188521064__en-us_topic_0238518398_en-us_topic_0237362203_en-us_topic_0202727281_image40743791111347" src="figure/en-us_image_0000001188681116.png"></span></div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_mt_0133.html">System Functions</a></div>
</div>
</div>