文档

CAST

更新时间:

MaxCompute CAST是数据类型转换函数,支持将一个表达式结果(expr)转换成目标数据类型(type)。本文为您介绍CAST函数的命令格式、参数说明以及使用示例。

命令格式

cast(<expr> as <type>)

参数说明

  • expr:必填。待转换数据源。

  • type:必填。目标数据类型。用法如下:

    • cast(double as bigint):将DOUBLE数据类型值转换成BIGINT数据类型。

    • cast(string as bigint):在将字符串转为BIGINT数据类型时,如果字符串中是以整型表达的数字,则会直接将它们转为BIGINT类型。如果字符串中是以浮点数或指数形式表达的数字,则会先转为DOUBLE数据类型,再转为BIGINT数据类型。

    • cast(string as datetime)cast(datetime as string):会采用默认的日期格式yyyy-mm-dd hh:mi:ss

    除此之外,cast还支持基本数据类型与JSON类型之间的相互转换,所支持的类型包括:JSON/STRING/BIGINT/INT/TINYINT/SMALLINT/DOUBLE/FLOAT/BOOLEAN/SQL-TYPE示例用法如下:

    • cast(json as string):将JSON表达式转换为STRING类型。JSON表达式要求为非ARRAY和OBJECT类型。

    • cast(string as json):将STRING类型值转换为JSON表达式,JSON表达式的类型为STRING。注意其与json_parse和json_format的区别,json_parse只支持合法的JSON STRING转成JSON,而且可以转成JSON OBJECT ,而cast(string as json) 可以将任意STRING转成JSON STRING,JSON类型是STRING。

    • cast(null as json):将NULL值转换为JSON类型。

    • cast(json 'null' as ...):json 'null'和null会转换成sql null。

返回值说明

  • 返回值为转换后的目标数据类型。

  • 如果设置了setproject odps.function.strictmode=false,则会返回字母前的数字。

  • 如果设置了setproject odps.function.strictmode=true,则会返回错误。

  • 当转化成Decimal类型时,如果设置了odps.sql.decimal.tostring.trimzero=true:去掉小数点后末尾的零;如果设置了odps.sql.decimal.tostring.trimzero=false:保留小数点后末尾的零。

    重要

    目前odps.sql.decimal.tostring.trimzero参数只对表里取数生效,对静态值不生效。

使用示例

  • 示例1:常见用法。命令示例如下。

    --返回1。
    select cast('1' as bigint);
  • 示例2:将STRING数据类型值转换成BOOLEAN数据类型,当STRING为空字符串时返回false,否则返回true。命令示例如下。

    • STRING为空字符串。

      select cast("" as boolean);
      --返回
      +------+
      | _c0  |
      +------+
      | false |
      +------+
    • STRING为非空字符串。

      select cast("false" as boolean);
      --返回true
      +------+
      | _c0  |
      +------+
      | true |
      +------+
  • 示例3:将字符串转换成日期。

    --将字符串转换成日期
    select cast("2022-12-20" as date);
    --返回
    +------------+
    | _c0        |
    +------------+
    | 2022-12-20 |
    +------------+
    
    --将带时分秒的日期字符串转成日期
    select cast("2022-12-20 00:01:01" as date);
    --返回
    +------------+
    | _c0        |
    +------------+
    | NULL       |
    +------------+
    --如果需要正常显示,需要设置以下参数:
    set odps.sql.executionengine.enable.string.to.date.full.format= true;
    select cast("2022-12-20 00:01:01" as date);
    --返回
    +------------+
    | _c0        |
    +------------+
    | 2022-12-20 |
    +------------+
    说明

    默认参数odps.sql.executionengine.enable.string.to.date.full.format的值为false,如果需要转换带时分秒的日期字符串,需要把该参数值设置成true

  • 示例4(错误命令示例):异常用法,如果转换不成功或遇到不支持的类型转换,会引发异常。错误命令示例如下。

    select cast('abc' as bigint);
  • 示例5:设置了setproject odps.function.strictmode=false的场景示例。

    setprojectodps.function.strictmode=false;
    select cast('123abc'as bigint);
    --返回
    +------------+
    |_c0|
    +------------+
    |123|
    +------------+
  • 示例6:设置了setproject odps.function.strictmode=true的场景示例。

    setprojectodps.function.strictmode=true;
    select cast('123abc' as bigint);
    --返回
    FAILED:ODPS-0130071:[0,0]Semanticanalysisexception-physicalplangenerationfailed:java.lang.NumberFormatException:ODPS-0123091:Illegaltypecast-Infunctioncast,value'123abc'cannotbecastedfromStringtoBigint.
  • 示例7:设置了odps.sql.decimal.tostring.trimzero的场景示例。

    --创建表
    create table mf_dot (dcm1 decimal(38,18),
                         dcm2 decimal(38,18));
    --insert数据
    insert into table mf_dot values (12.45500BD,12.3400BD);
    
    --Flag为true或者不设置时
    set odps.sql.decimal.tostring.trimzero=true;
    --去掉小数点后末尾的零
    select cast(round(dcm1,3) as string),cast(round(dcm2,3) as string) from mf_dot;
    --返回值
    +------------+------------+
    | _c0        | _c1        |
    +------------+------------+
    | 12.455     | 12.34      |
    +------------+------------+
    
    --Flag为false时
    set odps.sql.decimal.tostring.trimzero=false;
    --保留小数点后末尾的零
    select cast(round(dcm1,3) as string),cast(round(dcm2,3) as string) from mf_dot;
    --返回值
    +------------+------------+
    | _c0        | _c1        |
    +------------+------------+
    | 12.455     | 12.340     |
    +------------+------------+
    
    --对静态值不生效
    set odps.sql.decimal.tostring.trimzero=false;
    select cast(round(12345.120BD,3) as string);
    --返回:
    +------------+
    | _c0        |
    +------------+
    | 12345.12   |
    +------------+
  • 示例8:STRING和JSON类型相互转换。

    --json转成string
    select cast(json '123' as string);
    --返回:
    +-----+
    | _c0 |
    +-----+
    | 123 |
    +-----+
    
    --json转成string
    select cast(json '"abc"' as string);
    --返回:
    +-----+
    | _c0 |
    +-----+
    | abc |
    +-----+
    
    --json转成string
    select cast(json 'true' as string); 
    --返回:
    +-----+
    | _c0 |
    +-----+
    | TRUE |
    +-----+
    
    --json转成string
    select cast(json 'null' as string);
    --返回:
    +-----+
    | _c0 |
    +-----+
    | NULL |
    +-----+
    
    --string转成json
    select cast('{"a":2}' as json); 
    --返回:
    +-----+
    | _c0 |
    +-----+
    | "{\"a\":2}" |
    +-----+
    
    
    --json转成string的错误示例,不支持array/object类型的JSON表达式转换为string。
    select cast(json '{"a":2}' as string);
    --返回报错:
    FAILED: ODPS-0123091:Illegal type cast - Unsupported cast from json array/object to string
  • 示例9:NUMBER和JSON类型相互转换。

    --json转成bigint
    select cast(json '123' as bigint);
    --返回:
    +------------+
    | _c0        |
    +------------+
    | 123        |
    +------------+
    
    --json转成float
    select cast(json '"1.23"' as float);
    --返回:
    +------+
    | _c0  |
    +------+
    | 1.23 |
    +------+
    
    --json转成double
    select cast(json '1.23' as double);
    --返回:
    +------------+
    | _c0        |
    +------------+
    | 1.23       |
    +------------+
    
    --int转成json
    select cast(123 as json);
    --返回:
    +-----+
    | _c0 |
    +-----+
    | 123 |
    +-----+
    
    --float转成json
    select cast(1.23 as json);
    --返回:
    +-----+
    | _c0 |
    +-----+
    | 1.23 |
    +-----+
  • 示例10:BOOLEAN和JSON类型的相互转换。

    --boolean转成bigint
    select cast(true as json);
    --返回:
    +-----+
    | _c0 |
    +-----+
    | true |
    +-----+
    
    --json转成boolean
    select cast(json 'false' as boolean);
    --返回:
    +------+
    | _c0  |
    +------+
    | false |
    +------+
    
    --json转成boolean
    select cast(json '"abc"' as boolean);
    --返回:
    +------+
    | _c0  |
    +------+
    | true |
    +------+
    
    --array/object不能转成boolean
    select cast(json '[1,2]' as boolean);
    --返回报错:
    Unsupported cast from json array/object to boolean
  • 示例11:NULL和JSON类型的相互转换。

    --null转成string
    select json_type(cast(null as json));
    --返回:
    +-----+
    | _c0 |
    +-----+
    | NULL |
    +-----+

相关函数

CAST函数属于复杂类型函数,更多对复杂类型数据(例如ARRAY、MAP、STRUCT、JSON数据)的处理函数请参见复杂类型函数

同时,CAST函数也属于其他函数,更多其他业务场景的函数请参见其他函数

  • 本页导读 (1)
文档反馈