字符及处理方法1.固定长度字符串:char(n) 空格补足,效率高,最大2000.2.变长:varchar(n) 最大4000varchar2(n) 最大4000 向下兼容3.在变量声明中的字符类型:char(n) 大小:32767varchar(n),varchar2(n) 为:327674.字符型处理:4.1 向左补全字符串lpad(string,padded_length,[pad_string]) 函数4.2 向右补全字符串rpad(string,padded_length,[pad_string]) 函数4.3 小写返回lower()4.4 大写返回upper()4.5 单词首字符大写initcap()4.6 返回字符长度length()4.7 截取字符串substr(string,start_index,length) 第一个字符位置为14.8 获得字符串出现的位置instr(string ,sub_string, [,start_index] [,times])4.9 删除字符串左侧空格ltrim()4.10 删除字符串右侧空格rtrim()4.11 删除字符串两侧空格trim()4.12 串联字符串concat()select concat(ename,'_emp') from emp;串联方法2:select ename||'_emp'||deptno from emp;-----------||字符串连接符4.13 反转字符串reverse()数值及处理方法一、数值:number [(precision [,scale]]注:precision 为精度,scale 为指定小数后的位数。
根据四舍五入取精度。
对12345.977 精度为8,当精确到小数点后2位为12345.98 精度为7;而精确到小数点前2位的话为12300,精度为3。
对于0.000995 精度为3,精确到小数点后5位为0.00100,此时精度为3。
二、数值处理:2.1 返回数值的绝对值abs()2.2 返回数字的“四舍五入”round()2.3 向上取整ceil()2.4 向下取整floor()2.5 取模mod(被除数,除数)2.6 返回数字的正负性sign()注:小于0时返回-1,大于0时返回1,等于0时返回0.2.7 返回数字的平方根sqrt()2.8 乘方power(底数,指数)2.9 截取数字trunc(原数值[,小数位数])2.10 将ASCII 码转换为字符chr()如:select chr(65) character from dual;CHARACTER---------A2.11 格式化数值to_char(原数值,格式)格式可以为:0、9、,、FM、$、L、C。
(1)“0”select to_char(12.78,'000.000') result from dual;RESULT--------012.780(2)“9”* select to_char(12.78,'999.999') result from dual;RESULT--------12.780* select to_char(0.78,'999.999') result from dual;RESULT--------.780* select to_char(0.78,'990.999') result from dual;RESULT--------0.780(3)“,”select to_char(4560000,'99,999,990.00') result from dual;RESULT--------------4,560,000.00(4)“FM”——Format Maskselect to_char(12.78,'999.999') result ,to_char(12.78,'FM999.999') fm_result from dual;RESULT FM_RESULT-------- ---------12.780 12.78(5)“$”* select to_char(12.78,'$999.999') result from dual;RESULT---------$12.780* select to_char(12.78,'999.9$99') result from dual;RESULT---------$12.780但与FM一起时必须FM在$的开头:* select to_char(12.78,'FM$999.999') result from dual;RESULT---------$12.78(6)“L”select to_char(12.78,'FML999.999') result from dual;RESULT------------------¥12.78(7)“C”select to_char(12.78,'FM999.999C') result from dual;RESULT---------------12.78CNY(8)十进制转换为十六进制x :select to_char(255,'xxx') from dual;TO_CHAR(255,'XXX')------------------ff注:"xxx" 中的x位不能小于实际的十六进制数的位数如:select to_char(255,'x') from dual;TO_CHAR(255,'X')----------------日期及处理方法1.获取当前日期sysdate()SQL> select sysdate from dual;SYSDATE-----------2016-07-312.为日期加上特定月份add_months()注:如果超过日期返回最后一天。
SQL> select add_months(sysdate,1) from dual;ADD_MONTHS(SYSDATE,1)---------------------2016-07-31 PM 01:04:33.返回特定日期的最后一天last_day()SQL> select last_day(to_date('2016-06-28','YYYY-MM-DD')) new_date from dual;NEW_DATE-----------2016-06-294.返回两个日期所差的月数months_between()SQL>selectmonths_between(to_date('1988-7-4','YYYY-MM-DD'),to_date('2012-1-1','YYYY-MM-DD')) new_date from dual;NEW_DATE-----------281.903225.返回当前会话时区的当前日期current_date()SQL>select sessiontimezone,to_char(current_date,'yyyy-mm-dd hh:mi:ss') result from dual;SESSIONTIMEZONERESULT--------------------------------------------------------------------------- -------------------+08:00201-07-31 01:19:49注:+08:00 表明当前时区为东八区。
6.返回当前会话时区的时间戳current_timestamp()SQL> select sessiontimezone,current_timestamp from dual;SESSIONTIMEZONECURRENT_TIMESTAMP-----------------------------------------------------------------------------------------------------------------------------------------------------------+08:0001-1月-12 01.20.45.234000 下午+08:007.将日期转换为字符串to_char()SQL> select to_char(sysdate,'YYYY-MM-DD') new_date from dual;NEW_DATE----------2016-07-318.截取日期trunc(日期,截取格式)(1)将日期截到天:SQL> select trunc(to_date('2010-4-5','YYYY-MM-DD'),'DD') new_date from dual;NEW_DATE-----------2010-04-05(2)将日期截到月:SQL> select trunc(to_date('2010-4-5','YYYY-MM-DD'),'MM') new_date from dual;NEW_DATE-----------2010-04-01(3)将日期截到分钟:SQL> select trunc(sysdate,'MI') from dual;TRUNC(SYSDATE,'MI')-------------------2016-07-31 PM 01:40(4)trunc()函数只截到需要的部分,其它的全为开始值。
如:SQL> select to_char(trunc(sysdate,'DD'),'YYYY-MM-DD HH:MI:SS AM') new_date from dual;NEW_DATE------------------------2016-07-31 12:00:00 上午9.返回日期的某个域extract(域名from 日期)9.1 分离出月份SQL> select extract(month from sysdate) new_month from dual;NEW_MONTH----------79.2 分离出小时SQL> select extract(hour from systimestamp) new_hour from dual;NEW_HOUR----------5注:小时不能用sysdate ,而且用systimestamp 获取的并不是当前时区的时间,而是为零时区的标准时间。