doc-exports/docs/dli/sqlreference/dli_spark_locate.html
Su, Xiaomeng 76a5b1ee83 dli_sqlreference_20240227
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
Co-committed-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
2024-03-27 22:02:33 +00:00

72 lines
8.6 KiB
HTML

<a name="dli_spark_locate"></a><a name="dli_spark_locate"></a>
<h1 class="topictitle1">locate</h1>
<div id="body8662426"><p id="dli_spark_locate__en-us_topic_0000001648099150_p1560010331539">This function is used to return the position of substr in str. You can specify the starting position of your search using "start_pos," which starts from 1.</p>
<div class="section" id="dli_spark_locate__en-us_topic_0000001648099150_section45291954203217"><h4 class="sectiontitle">Syntax</h4><pre class="screen" id="dli_spark_locate__en-us_topic_0000001648099150_screen10637184423619">locate(string &lt;substr&gt;, string &lt;str&gt;[, bigint &lt;start_pos&gt;])</pre>
</div>
<div class="section" id="dli_spark_locate__en-us_topic_0000001648099150_section992014913317"><h4 class="sectiontitle">Parameters</h4>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dli_spark_locate__en-us_topic_0000001648099150_table1829154762513" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Parameters</caption><thead align="left"><tr id="dli_spark_locate__en-us_topic_0000001648099150_row8830104792517"><th align="left" class="cellrowborder" valign="top" width="19.220000000000002%" id="mcps1.3.3.2.2.5.1.1"><p id="dli_spark_locate__en-us_topic_0000001648099150_p983074711252">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="13.420000000000002%" id="mcps1.3.3.2.2.5.1.2"><p id="dli_spark_locate__en-us_topic_0000001648099150_p6830124732517">Mandatory</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="15.590000000000002%" id="mcps1.3.3.2.2.5.1.3"><p id="dli_spark_locate__en-us_topic_0000001648099150_p13318164512614">Type</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="51.77%" id="mcps1.3.3.2.2.5.1.4"><p id="dli_spark_locate__en-us_topic_0000001648099150_p08301547132513">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="dli_spark_locate__en-us_topic_0000001648099150_row199266231004"><td class="cellrowborder" valign="top" width="19.220000000000002%" headers="mcps1.3.3.2.2.5.1.1 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p16689132714493">str</p>
</td>
<td class="cellrowborder" valign="top" width="13.420000000000002%" headers="mcps1.3.3.2.2.5.1.2 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p1937912389241">Yes</p>
</td>
<td class="cellrowborder" valign="top" width="15.590000000000002%" headers="mcps1.3.3.2.2.5.1.3 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p183791038192411">STRING</p>
</td>
<td class="cellrowborder" valign="top" width="51.77%" headers="mcps1.3.3.2.2.5.1.4 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p99134255019">Target string to be searched for</p>
<p id="dli_spark_locate__en-us_topic_0000001648099150_p19379638122412">If the value is of the BIGINT, DOUBLE, DECIMAL, or DATETIME type, the value is implicitly converted to the STRING type for calculation. For other types of values, an error is reported.</p>
</td>
</tr>
<tr id="dli_spark_locate__en-us_topic_0000001648099150_row138061989595"><td class="cellrowborder" valign="top" width="19.220000000000002%" headers="mcps1.3.3.2.2.5.1.1 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p1197812363499">substr</p>
</td>
<td class="cellrowborder" valign="top" width="13.420000000000002%" headers="mcps1.3.3.2.2.5.1.2 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p1228018496242">Yes</p>
</td>
<td class="cellrowborder" valign="top" width="15.590000000000002%" headers="mcps1.3.3.2.2.5.1.3 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p127881156138">STRING</p>
</td>
<td class="cellrowborder" valign="top" width="51.77%" headers="mcps1.3.3.2.2.5.1.4 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p1980795014507">Substring to be matched</p>
<p id="dli_spark_locate__en-us_topic_0000001648099150_p784415518499">If the value is of the BIGINT, DOUBLE, DECIMAL, or DATETIME type, the value is implicitly converted to the STRING type for calculation. For other types of values, an error is reported.</p>
</td>
</tr>
<tr id="dli_spark_locate__en-us_topic_0000001648099150_row20280174932413"><td class="cellrowborder" valign="top" width="19.220000000000002%" headers="mcps1.3.3.2.2.5.1.1 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p2860810135914">start_pos</p>
</td>
<td class="cellrowborder" valign="top" width="13.420000000000002%" headers="mcps1.3.3.2.2.5.1.2 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p5508183295918">No</p>
</td>
<td class="cellrowborder" valign="top" width="15.590000000000002%" headers="mcps1.3.3.2.2.5.1.3 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p3859171019594">BIGINT</p>
</td>
<td class="cellrowborder" valign="top" width="51.77%" headers="mcps1.3.3.2.2.5.1.4 "><p id="dli_spark_locate__en-us_topic_0000001648099150_p1485911016594">Start position for the search</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="dli_spark_locate__en-us_topic_0000001648099150_section210162513312"><h4 class="sectiontitle">Return Values</h4><p id="dli_spark_locate__en-us_topic_0000001648099150_p1191215401502">The return value is of the BIGINT type.</p>
<div class="note" id="dli_spark_locate__en-us_topic_0000001648099150_note61422099395"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><ul id="dli_spark_locate__en-us_topic_0000001648099150_ul1221135114017"><li id="dli_spark_locate__en-us_topic_0000001648099150_li122111951405">If substr cannot be matched in str, <strong id="dli_spark_locate__en-us_topic_0000001648099150_b1060044619595">0</strong> is returned.</li><li id="dli_spark_locate__en-us_topic_0000001648099150_li172116511309">If the value of <strong id="dli_spark_locate__en-us_topic_0000001648099150_b194541345017">str</strong> or <strong id="dli_spark_locate__en-us_topic_0000001648099150_b74541347016">substr</strong> is <strong id="dli_spark_locate__en-us_topic_0000001648099150_b14541243020">NULL</strong>, <strong id="dli_spark_locate__en-us_topic_0000001648099150_b14549417018">NULL</strong> is returned.</li><li id="dli_spark_locate__en-us_topic_0000001648099150_li152118517017">If the value of <strong id="dli_spark_locate__en-us_topic_0000001648099150_b101434247014">start_pos</strong> is <strong id="dli_spark_locate__en-us_topic_0000001648099150_b21435241706">NULL</strong>, <strong id="dli_spark_locate__en-us_topic_0000001648099150_b014362419013">0</strong> is returned.</li></ul>
</div></div>
</div>
<div class="section" id="dli_spark_locate__en-us_topic_0000001648099150_section13277192233920"><h4 class="sectiontitle">Example Code</h4><ul id="dli_spark_locate__en-us_topic_0000001648099150_ul13630183114"><li id="dli_spark_locate__en-us_topic_0000001648099150_li6363218419">Search for the position of string <strong id="dli_spark_locate__en-us_topic_0000001648099150_b94707431104">ab</strong> in string <strong id="dli_spark_locate__en-us_topic_0000001648099150_b1247044314012">abhiab</strong>. An example command is as follows:<p id="dli_spark_locate__en-us_topic_0000001648099150_p118057191014">The value <strong id="dli_spark_locate__en-us_topic_0000001648099150_b158281644465617">1</strong> is returned.</p>
<pre class="screen" id="dli_spark_locate__en-us_topic_0000001648099150_screen732295131110">select locate('ab', 'abhiab');</pre>
<p id="dli_spark_locate__en-us_topic_0000001648099150_p161895716100">The value <strong id="dli_spark_locate__en-us_topic_0000001648099150_b706597174105210">5</strong> is returned.</p>
<pre class="screen" id="dli_spark_locate__en-us_topic_0000001648099150_screen83320105116">select locate('ab', 'abhiab', 2);</pre>
<p id="dli_spark_locate__en-us_topic_0000001648099150_p517457111016">The value <strong id="dli_spark_locate__en-us_topic_0000001648099150_b205776558454610">0</strong> is returned.</p>
<pre class="screen" id="dli_spark_locate__en-us_topic_0000001648099150_screen4141163118">select locate('ab', 'abhiab', null);</pre>
</li><li id="dli_spark_locate__en-us_topic_0000001648099150_li3363161820114">Search for the position of string <strong id="dli_spark_locate__en-us_topic_0000001648099150_b6916101219111">hi</strong> in string <strong id="dli_spark_locate__en-us_topic_0000001648099150_b191611213117">hanmeimei and lilei</strong>. An example command is as follows:<p id="dli_spark_locate__en-us_topic_0000001648099150_p0157173421115">The value <strong id="dli_spark_locate__en-us_topic_0000001648099150_b78128298154610">0</strong> is returned.</p>
<pre class="screen" id="dli_spark_locate__en-us_topic_0000001648099150_screen1355864116116">select locate('hi', 'hanmeimei and lilei');</pre>
</li></ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0472.html">String Functions</a></div>
</div>
</div>