sql 字符、数字类型自动转换及运算

本页面所有内容也可以在oracle 运行,只需要把int、float 、decimal 改为 number类型即可

-- 字符串转数字 int 类型

drop table test;
create table test(id int);
insert into test values(100);
insert into test values('100');

-- 字符串转数字 float 类型

drop table test;
create table test(id float(5,3));
insert into test values(10.99);
insert into test values('10.99');

-- 数字转字符串类型

drop table test;
create table test(id varchar(5));
insert into test values(100);
insert into test values('100');

-- 加减运算

-- int类型可以参与加减运算

drop table test;
create table test(id int);
insert into test values(100);
select id-10 from test;

-- double类型可以参与加减运算

drop table test;
create table test(id double);
insert into test values(100);
select id-10 from test;

-- decimal类型可以参与加减运算

drop table test;
create table test(id decimal);
insert into test values(100);
select id-10 from test;

-- 字符串类型也可以参与加减运算

drop table test;
create table test(id varchar(5));
insert into test values(100);
select id-10 from test;  -- 字符串也可以减,mysql会自动转型

-- 比较运算

-- int类型
drop table test;
create table test(id int);
insert into test values(100);
select * from test where id > 1;

drop table test;
create table test(id int);
insert into test values(100);
select * from test where id > '1';

-- 字符串类型

drop table test;
create table test(id varchar(5));
insert into test values(100);
select * from test where id > '1';

drop table test;
create table test(id varchar(5));
insert into test values(100);
select * from test where id > '中华人民共和国'; -- 语法正确,只是没有结果

转载于:https://www.cnblogs.com/Mike_Chang/p/9311748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值