码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-07-15 11:00:46      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:double   rom   字符串   参与   varchar   ora   转型   转换   table   

本页面所有内容也可以在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 > ‘中华人民共和国‘; -- 语法正确,只是没有结果

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

标签:double   rom   字符串   参与   varchar   ora   转型   转换   table   

原文地址:https://www.cnblogs.com/BaiLaowu/p/9311748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!