1. 程式人生 > >sql 字符、數字類型自動轉換及運算

sql 字符、數字類型自動轉換及運算

float 比較 轉型 oat 運行 數字 sel 只需要 decimal

本頁面所有內容也可以在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 字符、數字類型自動轉換及運算