当前位置:首页>开发>正文

oracle里nvl,to_char,decode这些函数怎么用啊 oracle 数据类型转换 to_char to_number to_date 使用方式!!

2023-06-21 07:58:00 互联网 未知 开发

 oracle里nvl,to_char,decode这些函数怎么用啊 oracle 数据类型转换 to_char to_number to_date 使用方式!!

oracle里nvl,to_char,decode这些函数怎么用啊?

Oracle提供了一些逻辑判断函数,这些函数可以在查询中使用。
1. nvl :针对空值进行测试
    函数原型为:NVL(testValue,SubstituteValue)
    常见的用法是  Select max(score) From SC Where Name=‘Jerry’
    有时max(score)为空,也就是说Jerry并没有考试记录,这时我们用"No Record"标注一下:
    Select NVL(max(score),"No Record") From SC
 
    还有一个NVL2函数跟其相似,函数原型为: NVL(testValue,SubValue1,SubValue2)
NVL2函数实现的是若testValue为NULL,返回SubValue1,否则返回SubValue2。
2.Decode函数
  Decode函数的原型为: Decode(testValue, if1, then1, if2,then2.....else).
   针对testValue进行测试,若testValue等于if1则返回then1,若testValue等于if2则返回then2,....若都没有返回,刚返回else. 示例如下:
若我们用Decode可以这样实现:

SELECT class, course,
       DECODE (student,
               A, Anco,
               B, Bily,
               C, Candy,
               D, Davi,
               E, Eve,
               F, Fion
              ) AS en_name
  FROM studentinfo
3. to_char函数功能,就是将数值型或者日期型转化为字符型,转换过程中可以进行格式化处理,函数原型较多,示例如下:


oracle 数据类型转换 to_char to_number to_date 使用方式!!

To_char:转换成字符串类型,如:To_char(1234.5, $9999.9),结果:$1234.5
To_date:转换成日期类型,如:To_date(1980-01-01, yyyy-mm-dd),结果:01-1月-80
To_number:转换成数值类型,如:To_number(1234.5),结果:1234.5

在oracle使用to_char函数查询emp表中每年入职的人数。

select to_char(hiredate,yyyy) year,count(*) number_of_person from emp group by to_char(hiredate,yyyy)

oracle数据库的 to char 和to date 区别

一、功能不同
1、to char:将时间日期按照指定的格式输出,得到的是字符串,而非date类型。
2、to  date:将字符串按照指定的格式输出,得到的是日期类型。
二、语法不同
1、to char: to_char(sysdate,yyyy-mm-dd hh24:mi:ss) 。
2、to  date:to_date(2004-05-07 13:23:44,yyyy-mm-dd hh24:mi:ss) 。

三、规则不同
1、to char:只要被转换的是一个日期,yyyy,mm,dd中间加不加连接符,加什么连接符都可以。
2、to  date:第一个参数的yyyy,mm,dd之间有没有连接符。如果有,那么第二个参数必须有连接符,随便是什么连接符。
参考资料来源:搜狗百科-to_char()
参考资料来源:搜狗百科-date函数

最新文章