测试lower/upper/initcap函数,使用dual哑表
- selectlower('www.BAIdu.COM')fromdual;
- selectupper('www.BAIdu.COM')fromdual;
- selectinitcap('www.BAIdu.COM')fromdual;
测试concat/substr函数,从1开始,表示字符,不论中英文
select concat('hello','你好') from dual;正确
select concat('hello','你好','世界') from dual;错误
select 'hello' || '你好' || '世界' from dual;正确
select concat('hello',concat('你好','世界')) from dual;正确
select substr('hello你好',5,3) from dual;
5表示从第几个字符开始算,第一个字符为1,中英文统一处理
3表示连续取几个字符
测试length/lengthb函数,编码方式为UTF8/GBK,一个中文占3/2个字节长度,一个英文一个字节
- selectlength('hello你好')fromdual;
- selectlengthb('hello你好')fromdual;
测试instr/lpad/rpad函数,从左向右找第一次出现的位置,从1开始
- selectinstr('helloworld','o')fromdual;
注意:找不到返回0,大小写敏感
- selectLPAD('hello',10,'#')fromdual;
- selectRPAD('hello','#')fromdual;
测试trim/replace函数
- selecttrim(''from'hell')fromdual;
- selectreplace('hello','l','L')fromdual;
测试round/trunc/mod函数作用于数值型
- selectround(3.1415,3)fromdual;
- selecttrunc(3.1415,3)fromdual;
- selectmod(10,3)fromdual;
当前日期:
- selectsysdatefromdual;
测试round作用于日期型(month)
- selectround(sysdate,'month')fromdual;
测试round作用于日期型(year)
- selectround(sysdate,'year')fromdual;
测试trunc作用于日期型(month)
- selecttrunc(sysdate,'month')fromdual;
测试trunc作用于日期型(year)
- selecttrunc(sysdate,'year')fromdual;
显示昨天,今天,明天的日期,日期类型 +- 数值 = 日期类型
- selectsysdate-1"昨天",sysdate"今天",sysdate+1"明天"fromdual;
以年和月形式显示员工近似工龄,日期-日期=数值,假设:一年以365天计算,一月以30天计算
- selectename"姓名",round(sysdate-hiredate,0)/365"工龄"fromemp;
使用months_between函数,精确计算到年底还有多少个月
- selectmonths_between('31-12月-16',sysdate)fromdual;
使用months_between函数,以精确月形式显示员工工龄
- selectename"姓名",months_between(sysdate,hiredate)"精确月工龄"fromemp;
测试add_months函数,下个月今天是多少号
- selectadd_months(sysdate,1)fromdual;
测试add_months函数,上个月今天是多少号
- selectadd_months(sysdate,-1)fromdual;
测试next_day函数,从今天开始算,下一个星期三是多少号【中文平台】
- selectnext_day(sysdate,'星期三')fromdual;
测试next_day函数,从今天开始算,下下一个星期三是多少号【中文平台】
- selectnext_day(next_day(sysdate,'星期三'),'星期三')fromdual;
测试next_day函数,从今天开始算,下一个星期三的下一个星期日是多少号【中文平台】
- selectnext_day(next_day(sysdate,'星期日')fromdual;
测试last_day函数,本月最后一天是多少号
- selectlast_day(sysdate)fromdual;
测试last_day函数,本月倒数第二天是多少号
- selectlast_day(sysdate)-1fromdual;
测试last_day函数,下一个月最后一天是多少号
- selectlast_day(add_months(sysdate,1))fromdual;
测试last_day函数,上一个月最后一天是多少号
- selectlast_day(add_months(sysdate,-1))fromdual;
注意:
1)日期-日期=天数
2)日期+-天数=日期