mysql类型转换
mysql 类型转换支持两种形式:
cast(expr as type)
convert(expr , type)
在mysql中,type支持的取值有以下值:
类型名 | 描述 |
---|---|
BINARY[(N)] | Produces a string with the VARBINARY data type |
CHAR[(N)] [charset_info] | Produces a string with the VARCHAR data type |
DATE | Produces a DATE value |
DATETIME[(M)] | Produces a DATETIME value |
DECIMAL[(M[,D])] | Produces a DECIMAL value |
JSON | Produces a JSON value |
NCHAR[(N)] | produces a string with the national character set. |
SIGNED | Produces a signed BIGINT value |
TIME[(M)] | Produces a TIME value. If the optional M value is given, it specifies the fractional seconds precision. |
UNSIGNED [INTEGER] | Produces an unsigned BIGINT value. |
根据上面表格可以知道,cast(expr as char)时,是将expr转换成varchar类型。
select cast('123456' as char); -- 返回123456
LightDB类型转换
LightDB类型方式有如下两种形式:
cast(expr as typename)
expr::typename
LightDB的规则比较简单,typename可以是所有类型的类型名。根据这一规则,cast(a as char[(n)] )的返回值是char类型,char不带长度在LightDB中是类型char(1)。因此,表达式不带长度是会出现截断,同时也会尾部空格。
为方便用户迁移,mysql模式下,23.4已兼容这一特性。
select cast('12342' as char);
-- 等价于
select cast('12342' as varchar);