Files
doc-exports/docs/dli/sqlreference/dli_08_15092.html
Su, Xiaomeng be9eabe464 dli_sqlreference_20250305
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>
2025-03-25 09:06:21 +00:00

162 lines
14 KiB
HTML

<a name="dli_08_15092"></a><a name="dli_08_15092"></a>
<h1 class="topictitle1">Type Conversion Functions</h1>
<div id="body0000001262495806">
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dli_08_15092__table177154895917" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Type conversion functions</caption><thead align="left"><tr id="dli_08_15092__row1371184814595"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.1.2.3.1.1"><p id="dli_08_15092__p177164875916">SQL Function</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.1.2.3.1.2"><p id="dli_08_15092__p10717486594">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="dli_08_15092__row671124818591"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.1.2.3.1.1 "><p id="dli_08_15092__p12719480596">CAST(value AS type)</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.1.2.3.1.2 "><p id="dli_08_15092__p11919101414015">Returns a new value that has been converted to the type.</p>
<p id="dli_08_15092__p15409916403">For example, <strong id="dli_08_15092__b4376921124719">CAST('42' AS INT)</strong> returns <strong id="dli_08_15092__b1415617252472">42</strong>;</p>
<p id="dli_08_15092__p57154815590"><strong id="dli_08_15092__b163981145154713">CAST(NULL AS VARCHAR)</strong> returns <strong id="dli_08_15092__b77821748154718">NULL</strong> of type <strong id="dli_08_15092__b174051751164718">VARCHAR</strong>.</p>
</td>
</tr>
<tr id="dli_08_15092__row771154813598"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.1.2.3.1.1 "><p id="dli_08_15092__p57104805918">TYPEOF(input) | TYPEOF(input, force_serializable)</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.1.2.3.1.2 "><p id="dli_08_15092__p97114481596">Returns a string representation of the data type of the input expression. By default, the returned string is a summary string that may omit some details for readability. If <strong id="dli_08_15092__b9129182874810">force_serializable</strong> is set to <strong id="dli_08_15092__b12810163124817">TRUE</strong>, the string representation can preserve the full data type that is stored in the catalog. Note that anonymous inline data types do not have a serializable string representation, and in this case, <strong id="dli_08_15092__b84603169498">NULL</strong> is returned.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="dli_08_15092__dli_08_0112_section9734433122713"><h4 class="sectiontitle">CAST Syntax Format</h4><pre class="screen" id="dli_08_15092__dli_08_0112_screen19396104742710">CAST(value AS type)</pre>
</div>
<div class="section" id="dli_08_15092__dli_08_0112_section197791153142712"><h4 class="sectiontitle">CAST Syntax Description</h4><p id="dli_08_15092__dli_08_0112_p125922369169">This syntax is used to forcibly convert types.</p>
</div>
<div class="section" id="dli_08_15092__dli_08_0112_section162182672816"><h4 class="sectiontitle">CAST Caveats</h4><p id="dli_08_15092__dli_08_0112_p16143105171613">If the input is <strong id="dli_08_15092__b204900876544337">NULL</strong>, <strong id="dli_08_15092__b211044674544337">NULL</strong> is returned.</p>
</div>
<div class="section" id="dli_08_15092__dli_08_0112_section6708122502812"><h4 class="sectiontitle">CAST Example 1: Converting the Amount Value to an Integer</h4><p id="dli_08_15092__dli_08_0112_a3e8ecbf3e3934ed1b04601e1689e0518">The following example converts the <strong id="dli_08_15092__b148927249544337">amount</strong> value to an integer.</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_s41c7e3601df241b281c5c3557d53415c">insert into temp select cast(amount as INT) from source_stream;</pre>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dli_08_15092__dli_08_0112_table178184319481" frame="border" border="1" rules="all"><caption><b>Table 2 </b>Examples of CAST type conversion functions</caption><thead align="left"><tr id="dli_08_15092__dli_08_0112_row681919314480"><th align="left" class="cellrowborder" valign="top" width="21.42%" id="mcps1.3.5.4.2.4.1.1"><p id="dli_08_15092__dli_08_0112_p1581993117483">Example</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="40.62%" id="mcps1.3.5.4.2.4.1.2"><p id="dli_08_15092__dli_08_0112_p881943112487">Description</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="37.96%" id="mcps1.3.5.4.2.4.1.3"><p id="dli_08_15092__dli_08_0112_p479454615014">Example</p>
</th>
</tr>
</thead>
<tbody><tr id="dli_08_15092__dli_08_0112_row38191317486"><td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.3.5.4.2.4.1.1 "><p id="dli_08_15092__dli_08_0112_p781913194814">cast(v1 as string)</p>
</td>
<td class="cellrowborder" valign="top" width="40.62%" headers="mcps1.3.5.4.2.4.1.2 "><p id="dli_08_15092__dli_08_0112_p13819531184820">Converts <strong id="dli_08_15092__b173975746244337">v1</strong> to a string. The value of <strong id="dli_08_15092__b205118173644337">v1</strong> can be of the numeric type or of the timestamp, date, or time type.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.5.4.2.4.1.3 "><p id="dli_08_15092__dli_08_0112_p395613491194">Table T1:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen179216204108">| content (INT) |
| ------------- |
| 5 |</pre>
<p id="dli_08_15092__dli_08_0112_p79562497911">Statement:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen12431554101019">SELECT
cast(content as varchar)
FROM
T1;</pre>
<p id="dli_08_15092__dli_08_0112_p199573491599">Result:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen13419104151112">"5"</pre>
</td>
</tr>
<tr id="dli_08_15092__dli_08_0112_row1917212119502"><td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.3.5.4.2.4.1.1 "><p id="dli_08_15092__dli_08_0112_p12173151185019">cast (v1 as int)</p>
</td>
<td class="cellrowborder" valign="top" width="40.62%" headers="mcps1.3.5.4.2.4.1.2 "><p id="dli_08_15092__dli_08_0112_p4174171105011">Converts <strong id="dli_08_15092__b93050572544337">v1</strong> to the <strong id="dli_08_15092__b172010696244337">int</strong> type. The value of <strong id="dli_08_15092__b176969663844337">v1</strong> can be a number or a character.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.5.4.2.4.1.3 "><p id="dli_08_15092__dli_08_0112_p18383418139">Table T1:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen116016486135">| content (STRING) |
| ------------- |
| "5" |</pre>
<p id="dli_08_15092__dli_08_0112_p6323481312">Statement:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen65451837201411">SELECT
cast(content as int)
FROM
T1;</pre>
<p id="dli_08_15092__dli_08_0112_p648345137">Result:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen3448717151">5</pre>
</td>
</tr>
<tr id="dli_08_15092__dli_08_0112_row12267712165017"><td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.3.5.4.2.4.1.1 "><p id="dli_08_15092__dli_08_0112_p026851219507">cast(v1 as timestamp)</p>
</td>
<td class="cellrowborder" valign="top" width="40.62%" headers="mcps1.3.5.4.2.4.1.2 "><p id="dli_08_15092__dli_08_0112_p14268512155012">Converts <strong id="dli_08_15092__b166431397444337">v1</strong> to the <strong id="dli_08_15092__b163993763044337">timestamp</strong> type. The value of <strong id="dli_08_15092__b19357682044337">v1</strong> can be of the <strong id="dli_08_15092__b111632707344337">string</strong>, <strong id="dli_08_15092__b98260950044337">date</strong>, or <strong id="dli_08_15092__b53732629444337">time</strong> type.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.5.4.2.4.1.3 "><p id="dli_08_15092__dli_08_0112_p27431739192212">Table T1:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen145085582216">| content (STRING) |
| ------------- |
| "2018-01-01 00:00:01" |</pre>
<p id="dli_08_15092__dli_08_0112_p117441639182213">Statement:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen83883113426">SELECT
cast(content as timestamp)
FROM
T1;</pre>
<p id="dli_08_15092__dli_08_0112_p5744103922217">Result:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen21051924174215">1514736001000</pre>
</td>
</tr>
<tr id="dli_08_15092__dli_08_0112_row77856825316"><td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.3.5.4.2.4.1.1 "><p id="dli_08_15092__dli_08_0112_p157872805310">cast(v1 as date)</p>
</td>
<td class="cellrowborder" valign="top" width="40.62%" headers="mcps1.3.5.4.2.4.1.2 "><p id="dli_08_15092__dli_08_0112_p37879815318">Converts <strong id="dli_08_15092__b14219408644337">v1</strong> to the <strong id="dli_08_15092__b197018237744337">date</strong> type. The value of <strong id="dli_08_15092__b76147469944337">v1</strong> can be of the <strong id="dli_08_15092__b18939565744337">string</strong> or <strong id="dli_08_15092__b131443161944337">timestamp</strong> type.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.5.4.2.4.1.3 "><p id="dli_08_15092__dli_08_0112_p1324241431">Table T1:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen86153834318">| content (TIMESTAMP) |
| ------------- |
| 1514736001000 |</pre>
<p id="dli_08_15092__dli_08_0112_p232454104310">Statement:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen10337163214457">SELECT
cast(content as date)
FROM
T1;</pre>
<p id="dli_08_15092__dli_08_0112_p5324144164318">Result:</p>
<pre class="screen" id="dli_08_15092__dli_08_0112_screen54891750134716">"2018-01-01"</pre>
</td>
</tr>
</tbody>
</table>
</div>
<div class="note" id="dli_08_15092__dli_08_0112_note035781042910"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="dli_08_15092__dli_08_0112_p43681310162918">Flink jobs do not support the conversion of <strong id="dli_08_15092__b150066029244337">bigint</strong> to <strong id="dli_08_15092__b141111968144337">timestamp</strong> using CAST. You can convert it using <strong id="dli_08_15092__b155034347344337">to_timestamp</strong>.</p>
</div></div>
</div>
<div class="section" id="dli_08_15092__dli_08_0112_section14655163113329"><h4 class="sectiontitle">CAST Example 2</h4><ol id="dli_08_15092__ol154981922165916"><li id="dli_08_15092__li1049862220596">Create a Flink OpenSource SQL job by referring to <a href="dli_08_15058.html">Kafka</a> and <a href="dli_08_15060.html">Print</a>, enter the following job running script, and submit the job.<div class="p" id="dli_08_15092__p068291610113">When you create a job, set <strong id="dli_08_15092__b0421051125319">Flink Version</strong> to <strong id="dli_08_15092__b19432105115539">1.15</strong> in the <strong id="dli_08_15092__b194384519531">Running Parameters</strong> tab. Select <strong id="dli_08_15092__b104443511537">Save Job Log</strong>, and specify the OBS bucket for saving job logs. Change the values of the parameters in bold in the following script according to the actual situation.<pre class="screen" id="dli_08_15092__screen375516121614">CREATE TABLE kafkaSource (
cast_int_to_string int,
cast_String_to_int string,
case_string_to_timestamp string,
case_timestamp_to_date timestamp
) WITH (
'connector' = 'kafka',
'topic' = '<em id="dli_08_15092__i4543155265320"><strong id="dli_08_15092__b1754316528535">Kafka</strong></em><em id="dli_08_15092__i15341759195319"><strong id="dli_08_15092__b5227105913538">Topic</strong></em>',
'properties.bootstrap.servers' = '<em id="dli_08_15092__i9319103215313"><strong id="dli_08_15092__b0322115133213">KafkaAddress1:KafkaPort,KafkaAddress2:KafkaPort</strong></em>',
'properties.group.id' = '<em id="dli_08_15092__i14535174011311"><strong id="dli_08_15092__b1466610178329">GroupId</strong></em>',
'scan.startup.mode' = 'latest-offset',
"format" = "json"
);
CREATE TABLE printSink (
cast_int_to_string string,
cast_String_to_int int,
case_string_to_timestamp timestamp,
case_timestamp_to_date date
) WITH (
'connector' = 'print'
);
insert into printSink select
cast(cast_int_to_string as string),
cast(cast_String_to_int as int),
cast(case_string_to_timestamp as timestamp),
cast(case_timestamp_to_date as date)
from kafkaSource;</pre>
</div>
</li><li id="dli_08_15092__li249822219590">Connect to the Kafka cluster and send the following test data to the Kafka topic:<pre class="screen" id="dli_08_15092__screen7222143315544">{"cast_int_to_string":"1", "cast_String_to_int": "1", "case_string_to_timestamp": "2022-04-02 15:00:00", "case_timestamp_to_date": "2022-04-02 15:00:00"}</pre>
</li><li id="dli_08_15092__li9498172216599">View output.<ul id="dli_08_15092__ul133191514102210"><li id="dli_08_15092__li11970649381">Method 1:<ol type="a" id="dli_08_15092__ol4711275385"><li id="dli_08_15092__li5612141010388">Log in to the DLI management console and choose Job Management &gt; Flink Streaming Jobs.</li><li id="dli_08_15092__li117110711383">Locate the row that contains the target Flink job, and choose More &amp; &gt; FlinkUI in the Operation column.</li><li id="dli_08_15092__li07833554385">On the Flink UI, choose Task Managers, click the task name, and select Stdout to view the job run logs.</li></ol>
</li><li id="dli_08_15092__li341910155285">Method 2: If you select <strong id="dli_08_15092__b584415191897">Save Job Log</strong> on the <strong id="dli_08_15092__b98451919592">Running Parameters</strong> tab before submitting the job, perform the following operations:<ol type="a" id="dli_08_15092__ol864115198285"><li id="dli_08_15092__li10901621122819">Log in to the DLI management console and choose Job Management &gt; Flink Streaming Jobs.</li><li id="dli_08_15092__li1912163912282">Click the name of the corresponding Flink job, choose Run Log, click OBS Bucket, and locate the folder of the corresponding log based on the job running date.</li><li id="dli_08_15092__li0641191914285">Go to the folder of the corresponding date, find the folder whose name contains taskmanager, download the taskmanager.out file, and view the result log.</li></ol>
</li></ul>
<div class="p" id="dli_08_15092__p1585111204015">The query result is as follows:<pre class="screen" id="dli_08_15092__screen109768515712">+I(1,1,2022-04-02T15:00,2022-04-02)</pre>
</div>
</li></ol>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_15085.html">Built-In Functions</a></div>
</div>
</div>