首页 > 其他分享 >13.转换函数

13.转换函数

时间:2023-02-19 12:44:48浏览次数:40  
标签:00 转换 函数 sal -- CLERK 13 2023 ----------

1.隐式与显示数据转换

--在表达式中Oracle服务器能自动转换
-- from  varchar2 or char  to  number
-- from  varchar2 or char  to  date
-- from  number to varchar2 or char
-- from  date   to varchar2 or char

2.处理日期函数

--必须用单引号引起来
--区分大小写
--可用有效日期格式元素
--能去除填充的空格或前置的零
--用逗号与日期隔开
hr@ORCLPDB01 2023-02-19 11:59:00> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss am') from dual; 

TO_CHAR(SYSDATE,'YYYY-
----------------------
2023-02-19 11:59:00 am

Elapsed: 00:00:00.00
hr@ORCLPDB01 2023-02-19 11:59:00> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss AM') from dual;

TO_CHAR(SYSDATE,'YYYY-
----------------------
2023-02-19 11:59:01 AM

Elapsed: 00:00:00.00
-- FM 去除开头和结尾的空格
hr@ORCLPDB01 2023-02-19 11:59:01> select to_char(sysdate,'Day,"the" ddth "of" Month,yyyy') from dual;

TO_CHAR(SYSDATE,'DAY,"THE"DDTH"OF"MONTH,YYYY')
------------------------------------------------------------------------------------------
Sunday	 ,the 19th of February ,2023

Elapsed: 00:00:00.00
hr@ORCLPDB01 2023-02-19 12:00:23> select to_char(sysdate,'FMDay,"the" ddth "of" Month,yyyy') from dual;

TO_CHAR(SYSDATE,'FMDAY,"THE"DDTH"OF"MONTH,YYYY')
------------------------------------------------------------------------------------------
Sunday,the 19th of February,2023

Elapsed: 00:00:00.01

 

--使用to_char函数对数字进行转换
scott@ORCLPDB01 2023-02-19 12:02:24> select sal,to_char(sal,'$99,999.00') from emp;

       SAL TO_CHAR(SAL
---------- -----------
       800     $800.00
      1600   $1,600.00
      1250   $1,250.00
      2975   $2,975.00
      1250   $1,250.00
      2850   $2,850.00
      2450   $2,450.00
      3000   $3,000.00
      5000   $5,000.00
      1500   $1,500.00
      1100   $1,100.00
       950     $950.00
      3000   $3,000.00
      1300   $1,300.00

14 rows selected.

Elapsed: 00:00:00.01

 

--to_number 将字符串转换为数字格式
scott@ORCLPDB01 2023-02-19 12:02:26> select to_number('$12,345.00','$99,999.00') from dual;

TO_NUMBER('$12,345.00','$99,999.00')
------------------------------------
			       12345

Elapsed: 00:00:00.00

scott@ORCLPDB01 2023-02-19 12:08:04> select * from emp where hiredate>=to_date('1987-05-01','yyyy-mm-dd');

     EMPNO ENAME      JOB	       MGR HIREDATE		      SAL	COMM	 DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7876 ADAMS      CLERK	      7788 1987-05-23 00:00:00	     1100		     20

Elapsed: 00:00:00.00

3.空值函数

--适合空值函数如下:
--nvl(expr1,expr2)
--如果expr1为空,返回expr2,否则返回expr1
hr@ORCLPDB01 2023-02-19 12:16:10> select last_name,salary,nvl(commission_pct,0),(salary*12)+(salary*12*nvl(commission_pct,0)) an_sal
  2  from employees;

LAST_NAME		      SALARY NVL(COMMISSION_PCT,0)     AN_SAL
------------------------- ---------- --------------------- ----------
King			       24000			 0     288000
Kochhar 		       17000			 0     204000
De Haan 		       17000			 0     204000
Hunold				9000			 0     108000
Ernst				6000			 0	72000
--nvl2(expr1,expr2,expr3)
--如果expr1不为空返回expr2,为空则返回expr3
hr@ORCLPDB01 2023-02-19 12:17:35> select last_name,salary,commission_pct,
  2  nvl2(commission_pct,'sal+comm','sal') income
  3  from employees 
  4  where department_id in (50,80);

LAST_NAME		      SALARY COMMISSION_PCT INCOME
------------------------- ---------- -------------- --------
Weiss				8000		    sal
Fripp				8200		    sal
Kaufling			7900		    sal
Vollman 			6500		    sal
Mourgos 			5800		    sal
Nayer				3200		    sal
Mikkilineni			2700		    sal
Landry				2400		    sal
Markle				2200		    sal
Bissot				3300		    sal
Atkinson			2800		    sal
Marlow				2500		    sal
Olson				2100		    sal
Mallin				3300		    sal
Rogers				2900		    sal
Gee				2400		    sal
Philtanker			2200		    sal
Ladwig				3600		    sal
Stiles				3200		    sal
Seo				2700		    sal
Patel				2500		    sal
Rajs				3500		    sal
Davies				3100		    sal
Matos				2600		    sal
Vargas				2500		    sal
Russell 		       14000		 .4 sal+comm
Partners		       13500		 .3 sal+comm
Errazuriz		       12000		 .3 sal+comm

--nullif(expr1,expr2)
--如果相同返回空,否则返回expr1
scott@ORCLPDB01 2023-02-19 12:25:11>select ename,job,length(ename) n1,length(job) n2, nullif(length(ename),length(job)) from emp; 

ENAME	   JOB		     N1 	N2 NULLIF(LENGTH(ENAME),LENGTH(JOB))
---------- --------- ---------- ---------- ---------------------------------
SMITH	   CLERK	      5 	 5
ALLEN	   SALESMAN	      5 	 8				   5
WARD	   SALESMAN	      4 	 8				   4
JONES	   MANAGER	      5 	 7				   5
MARTIN	   SALESMAN	      6 	 8				   6
BLAKE	   MANAGER	      5 	 7				   5
CLARK	   MANAGER	      5 	 7				   5
SCOTT	   ANALYST	      5 	 7				   5
KING	   PRESIDENT	      4 	 9				   4
TURNER	   SALESMAN	      6 	 8				   6
ADAMS	   CLERK	      5 	 5
JAMES	   CLERK	      5 	 5
FORD	   ANALYST	      4 	 7				   4
MILLER	   CLERK	      6 	 5				   6

14 rows selected.

--coalesce(expr1,expr2,...,exprn)
--如果第一个表达式为非空,就返回该表达式,如果时空值,就返回第二个表达式,如果前两个表达式为空,就返回第三个表达式,依次类推,就返回第n个。
hr@ORCLPDB01 2023-02-19 12:28:38> select last_name,employee_id,
  2  coalesce(to_char(commission_pct),to_char(manager_id),'No commission and no manager')
  3  from employees;

LAST_NAME		  EMPLOYEE_ID COALESCE(TO_CHAR(COMMISSION_PCT),TO_CHAR
------------------------- ----------- ----------------------------------------
King				  100 No commission and no manager
Kochhar 			  101 100
De Haan 			  102 100

 4.条件表达式

--case语法
--第一种写法
scott@ORCLPDB01 2023-02-19 12:35:09> 
select empno,ename,sal,job, 
 case job 
 when 'ANALYST' then sal*1.1 
 when 'CLERK'   then sal*1.15 
 when 'MANAGER' then sal*1.2 
 else                sal*1.25 
 end new_sal 
from emp order by job; 

     EMPNO ENAME	     SAL JOB	      NEW_SAL
---------- ---------- ---------- --------- ----------
      7788 SCOTT	    3000 ANALYST	 3300
      7902 FORD 	    3000 ANALYST	 3300
      7934 MILLER	    1300 CLERK		 1495
      7900 JAMES	     950 CLERK	       1092.5
      7369 SMITH	     800 CLERK		  920
      7876 ADAMS	    1100 CLERK		 1265
      7698 BLAKE	    2850 MANAGER	 3420
      7566 JONES	    2975 MANAGER	 3570
      7782 CLARK	    2450 MANAGER	 2940
      7839 KING 	    5000 PRESIDENT	 6250
      7844 TURNER	    1500 SALESMAN	 1875
      7654 MARTIN	    1250 SALESMAN      1562.5
      7521 WARD 	    1250 SALESMAN      1562.5
      7499 ALLEN	    1600 SALESMAN	 2000

14 rows selected.

Elapsed: 00:00:00.00

--第二种写法
select empno,ename,sal,job, 
 case when job='ANALYST' then sal*1.1 
      when job='CLERK'   then sal*1.15 
      when job='MANAGER' then sal*1.2 
      else                    sal*1.25 
 end new_sal 
 from emp order by job;

     EMPNO ENAME	     SAL JOB	      NEW_SAL
---------- ---------- ---------- --------- ----------
      7788 SCOTT	    3000 ANALYST	 3300
      7902 FORD 	    3000 ANALYST	 3300
      7934 MILLER	    1300 CLERK		 1495
      7900 JAMES	     950 CLERK	       1092.5
      7369 SMITH	     800 CLERK		  920
      7876 ADAMS	    1100 CLERK		 1265
      7698 BLAKE	    2850 MANAGER	 3420
      7566 JONES	    2975 MANAGER	 3570
      7782 CLARK	    2450 MANAGER	 2940
      7839 KING 	    5000 PRESIDENT	 6250
      7844 TURNER	    1500 SALESMAN	 1875
      7654 MARTIN	    1250 SALESMAN      1562.5
      7521 WARD 	    1250 SALESMAN      1562.5
      7499 ALLEN	    1600 SALESMAN	 2000

14 rows selected.

Elapsed: 00:00:00.00

--decode语法
scott@ORCLPDB01 2023-02-19 12:32:48> select empno,ename,job,sal, 
 decode(job,'ANALYST', sal*1.1, 
            'CLERK',   sal*1.15, 
            'MANAGER', sal*1.20, 
                       sal*1.25) 
            new_sal 
  7    from emp order by job;

     EMPNO ENAME      JOB	       SAL    NEW_SAL
---------- ---------- --------- ---------- ----------
      7788 SCOTT      ANALYST	      3000	 3300
      7902 FORD       ANALYST	      3000	 3300
      7934 MILLER     CLERK	      1300	 1495
      7900 JAMES      CLERK	       950     1092.5
      7369 SMITH      CLERK	       800	  920
      7876 ADAMS      CLERK	      1100	 1265
      7698 BLAKE      MANAGER	      2850	 3420
      7566 JONES      MANAGER	      2975	 3570
      7782 CLARK      MANAGER	      2450	 2940
      7839 KING       PRESIDENT       5000	 6250
      7844 TURNER     SALESMAN	      1500	 1875
      7654 MARTIN     SALESMAN	      1250     1562.5
      7521 WARD       SALESMAN	      1250     1562.5
      7499 ALLEN      SALESMAN	      1600	 2000

14 rows selected.

Elapsed: 00:00:00.00

 

标签:00,转换,函数,sal,--,CLERK,13,2023,----------
From: https://www.cnblogs.com/yuanzijian/p/17134568.html

相关文章

  • jstl 函数
         下面是JSTL中自带的方法列表以及其描述函数名函数说明使用举例fn:contains判断字符串是否包含另外一个字符串<c:iftest="${fn:contains(name,searchString)}">......
  • oracle 10g+ 行列转换
    WITHtAS(SELECT1ASsno,1AScno,5ASgradeFROMdualUNIONALLSELECT1,2,6FROMdualUNIONALLSELECT1,3,10FROMdualUNIONALLSELE......
  • 在线将普通表与分区表进行相互转换以及移动数据文件位置
    文档课题:在线将普通表与分区表进行相互转换以及移动数据文件位置.数据库:oracle19.31、测试数据C:\Users\Administrator>sqlplus/assysdbaSQL*Plus:Release19.0.0.0.0......
  • 将古老的ASP项目转换为PHP初探
    ASP是一种服务器端脚本语言,主要用于开发动态Web应用程序。ASP可以在服务器上执行代码,并将结果返回给客户端浏览器,实现动态生成Web页面的功能。ASP代码通常包含在<%......
  • 12.单行函数
    1.单行函数--使用函数是为了操作数据--将输入的变量处理,每行返回一个结果--处理返回的每一行--一行返回一个结果--可以转化数据类型--能嵌套使用--传入的变量可以......
  • 代码随想录算法训练营day23 | leetcode 669. 修剪二叉搜索树 ● 108.将有序数组转换
    LeetCode669.修剪二叉搜索树分析1.0递归遍历树时删除符合条件(不在区间中)的节点-如何遍历如何删除如果当前节点大于范围,递归左树,反之右树当前节点不在范围内,删除它,把......
  • 「CF1392H」ZS Shuffles Cards
    题目点这里看题目。你有\(n+m\)张牌,其中有恰好\(n\)张为数字牌,分别标有\(1,2,3,\dots,n\),剩下的恰好\(m\)张均为鬼牌。一开始,牌被随机打乱,同时你有一个集合\(......
  • [LeetCode] 1331. Rank Transform of an Array 数组序号转换
    Givenanarrayofintegers arr,replaceeachelementwithitsrank.Therankrepresentshowlargetheelementis.Therankhasthefollowingrules:Rankis......
  • Django Rest Frame work 如何使用serializers序列化函数新手教程
    DjangoRestFramework如何使用serializers序列化   DjangoRestFramework提供了serializers模块,用于序列化和反序列化模型实例以及原生数据类型......
  • serializers序列化函数简单入门
    1.创建Django项目和应用程序首先,我们需要创建一个Django项目和一个Django应用程序。如果你已经有了Django项目和应用程序,请跳过这一步。$django-adminstartprojectm......