cast 是进行类型转换的, 可以针对各种Oracle数据类型. 修改的是用户的数据类型
例:
SQL> create table t1(a varchar(10));
Table created.
SQL> insert into t1 values ('12.3456');
1 row created.
SQL> select round(a) from t1;
ROUND(A)
----------
12
SQL> select round(a,3) from t1;
ROUND(A,3)
----------
12.346
SQL> select cast(a as int) from t1;
CAST(AASINT)
------------
12
SQL> select cast(a as number(8,4)) from t1;
CAST(AASNUMBER(8,4))
--------------------
12.3456
转换为“decimal”类型,如下:
select Cast(1.888888 as decimal) from dual;
----------------------------------
| Cast(1.888888 as decimal) |
----------------------------------
| 2 |
----------------------------------
select cast(1.88888 as decimal(12,2)) from dual;
----------------------------------
| Cast(1.88888 as decimal) |
----------------------------------
| 1.89 |
----------------------------------
========================================================
[b][color=red]round[/color][/b]只是修改的数据显示格式. 对数据做四舍五入. 类似的函数还有[b][color=red]ceil[/color][/b](取此数据的最小整数). [b]trunc[/b][color=red][/color]( 取整函数.)
复制内容到剪贴板
代码:
SQL> select round(12.45) round,trunc(12.45) trunc,ceil(12.45) ceil
2 from dual
3 /
ROUND TRUNC CEIL
---------- ---------- ----------
12 12 13
SQL> select round(12.54) round,trunc(12.54) trnc,ceil(12.54) ceil
2 from dual
3 /
ROUND TRNC CEIL
---------- ---------- ----------
13 12 13
SQL>
例:
SQL> create table t1(a varchar(10));
Table created.
SQL> insert into t1 values ('12.3456');
1 row created.
SQL> select round(a) from t1;
ROUND(A)
----------
12
SQL> select round(a,3) from t1;
ROUND(A,3)
----------
12.346
SQL> select cast(a as int) from t1;
CAST(AASINT)
------------
12
SQL> select cast(a as number(8,4)) from t1;
CAST(AASNUMBER(8,4))
--------------------
12.3456
转换为“decimal”类型,如下:
select Cast(1.888888 as decimal) from dual;
----------------------------------
| Cast(1.888888 as decimal) |
----------------------------------
| 2 |
----------------------------------
select cast(1.88888 as decimal(12,2)) from dual;
----------------------------------
| Cast(1.88888 as decimal) |
----------------------------------
| 1.89 |
----------------------------------
========================================================
[b][color=red]round[/color][/b]只是修改的数据显示格式. 对数据做四舍五入. 类似的函数还有[b][color=red]ceil[/color][/b](取此数据的最小整数). [b]trunc[/b][color=red][/color]( 取整函数.)
复制内容到剪贴板
代码:
SQL> select round(12.45) round,trunc(12.45) trunc,ceil(12.45) ceil
2 from dual
3 /
ROUND TRUNC CEIL
---------- ---------- ----------
12 12 13
SQL> select round(12.54) round,trunc(12.54) trnc,ceil(12.54) ceil
2 from dual
3 /
ROUND TRNC CEIL
---------- ---------- ----------
13 12 13
SQL>