Oracle的trunc函数与truncate函数都可以对数值进行截取操作
-- 首先看对数值进行截取操作
SELECT TRUNCATE(122.123, 4) from dual; # 122.123
SELECT TRUNCATE(122.123, 3) from dual; # 122.123
SELECT TRUNCATE(122.123, 2) from dual; # 122.12
SELECT TRUNCATE(122.123, 1) from dual; # 122.1
SELECT TRUNCATE(122.123, 0) from dual; # 122
SELECT TRUNCATE(122.123, -1) from dual; # 120
SELECT TRUNCATE(122.123, -2) from dual; # 100
SELECT TRUNCATE(122.123, -3) from dual; # 0
SELECT TRUNCATE(122.123, -4) from dual; # 0
--trunc函数
SELECT TRUNC(122.123, 4) from dual; # 122.123
SELECT TRUNC(122.123, 3) from dual; # 122.123
SELECT TRUNC(122.123, 2) from dual; # 122.12
SELECT TRUNC(122.123, 1) from dual; # 122.1
SELECT TRUNC(122.123, 0) from dual; # 122
SELECT TRUNC(122.123, -1) from dual; # 120
SELECT TRUNC(122.123, -2) from dual; # 100
SELECT TRUNC(122.123, -3) from dual; # 0
SELECT TRUNC(122.123, -4) from dual; # 0
在数值操作上,trunc和truncate是一模一样的效果
但除此之外,trunc能够对日期进行操作,而truncate不能
--
SELECT TRUNC(sysdate, 'YY') from dual; # 01-1月 -22
SELECT TRUNC(sysdate, 'yy') from dual; # 01-1月 -22
SELECT TRUNC(sysdate, 'MM') from dual; # 01-2月 -22
SELECT TRUNC(sysdate, 'mm') from dual; # 01-2月 -22
SELECT TRUNC(sysdate, 'DD') from dual; # 24-2月 -22
SELECT TRUNC(sysdate, 'dd') from dual; # 24-2月 -22
|