Oracle单行函数和多行函数实例


单行函数和多行函数示意图:

 

单行函数分为五种类型:字符函数、数值函数、日期函数、转换函数、通用函数

单行函数:

  1. --大小写控制函数   
  2. select lower('Hello World') 转小写, upper('Hello World') 转大写 from dual;  
  3. --initcap: 首字母大写   
  4. select initcap('hello world') 首字符大写 from dual;  
  5.   
  6. --字符控制函数   
  7. -- concat: 字符连接函数, 等同于  ||   
  8. select concat('Hello',' World'from dual;  
  9. --substr:求母串中的某个子串   
  10. select substr('Hello World',3) from dual;  
  11. select substr('Hello World',3,4) from dual;  
  12. --length和lengthb: 字符数和字节数   
  13. select length('China') 字符数, lengthb('China') 字节数  from dual;  
  14. --instr:在母串中,查找子串的位置   
  15. select instr('Hello World','ll'from dual;  
  16. --lpad,rpad: 左右填充,将abcd用*填充到10位   
  17. select lpad('abcd',10,'*') 左填充, rpad('abcd',10,'*') 右填充 from dual;  
  18. --trim: 去掉字符串前后指定的字符   
  19. select trim('H' from 'Hello WorldH'from dual;  
  20. --replace:字符串替换函数   
  21. select replace('Hello Wordl','l','*'from dual;  
  22.   
  23. --数字函数   
  24. select round(45.926,2) 四舍五入, trunc(45.926,2)  截断 ,mod(1600,300) 求于 from dual;  
  25. --ROUND函数   
  26. select round(45.923,0) 整数位, round(45.923,-1) 十位,round(45.923,-2) 百位 from dual;  
  27.   
  28. --日期函数   
  29. --显示当前日期   
  30. select sysdate from dual;  
  31. --显示时间部分   
  32. select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'from dual;  
  33. --显示昨天,今天和明天,加减数字仍未日期   
  34. select sysdate-1 昨天, sysdate 今天, sysdate+1 明天 from dual;  
  35. --两个日期相减,结果为相差的天数,查询员工信息,显示员工工龄。两个日期不能相加   
  36. select empno,ename, sysdate-hiredate 天 from emp;  
  37. --查询员工信息,显示员工工龄,分别按照天,星期,月显示   
  38. select empno,ename,sysdate-hiredate 天,(sysdate-hiredate)/7 星期, (sysdate-hiredate)/30 月 from emp;  
  39. --months_between:两个日期相差的月数   
  40. select (sysdate-hiredate)/30 方式一, months_between(sysdate,hiredate) 方式二 from emp;  
  41. --add_months:在指定日期上加上若干个月   
  42. select add_months(sysdate,1)  下个月, add_months(sysdate,123) "123个月后" from dual  
  43. --last_day: 某个日期当月的最后一天   
  44. select last_day(sysdate) from dual;  
  45. --next_day:下周六   
  46. select next_day(sysdate,'星期五'from dual;  
  47. --对日期进行四舍五入   
  48. select round(sysdate,'MONTH')  月,round(sysdate,'YEAR'from dual;     
  49. --对日期进行截断   
  50. select trunc(sysdate,'MONTH')  月,trunc(sysdate,'YEAR'from dual;  
  51. --日期格式   
  52. select * from emp where hiredate=to_date('1982-01-23','yyyy-mm-dd');  
  53. -- 查询当前日期:显示:  2011-09-17 15:12:15今天是星期六   
  54. select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss"今天是"day'from dual;  
  55. --查询员工信息,显示员工的编号,姓名,月薪,要求有货币代码(L),千位符(,),小数点(.),   
  56. select empno,ename,to_char(sal,'L9,999.99'from emp;  
  • 1
  • 2
  • 下一页

相关内容