【LightDB】cast as char兼容

本文主要介绍了MySQL和LightDB中的类型转换方法,包括cast和convert函数在MySQL中的用法,以及LightDB中cast(exprastypename)的简单规则。特别提到MySQL23.4版本对字符类型处理的兼容性改进。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
DATEProduces a DATE value
DATETIME[(M)]Produces a DATETIME value
DECIMAL[(M[,D])]Produces a DECIMAL value
JSONProduces a JSON value
NCHAR[(N)]produces a string with the national character set.
SIGNEDProduces 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);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值