<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Oracle on 月盾的博客</title>
    <link>https://blog.hopefly.top/tags/oracle/</link>
    <description>Recent content in Oracle on 月盾的博客</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Wed, 05 Nov 2014 08:16:19 +0000</lastBuildDate>
    <atom:link href="https://blog.hopefly.top/tags/oracle/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>oracle decode和nvl的用法</title>
      <link>https://blog.hopefly.top/blogdetail/5459dcd3bf93a1aa252561fe/</link>
      <pubDate>Wed, 05 Nov 2014 08:16:19 +0000</pubDate>
      <guid>https://blog.hopefly.top/blogdetail/5459dcd3bf93a1aa252561fe/</guid>
      <description>&lt;p&gt;Oracle 中 decode 函数用法&lt;/p&gt;&#xA;&lt;p&gt;含义解释：&#xA;decode(条件,值1,返回值1,值2,返回值2,&amp;hellip;值n,返回值n,缺省值)&lt;/p&gt;&#xA;&lt;p&gt;该函数的含义如下：&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;IF 条件=值1 THEN&#xA;　　　　RETURN(翻译值1)&#xA;ELSIF 条件=值2 THEN&#xA;　　　　RETURN(翻译值2)&#xA;　　　　......&#xA;ELSIF 条件=值n THEN&#xA;　　　　RETURN(翻译值n)&#xA;ELSE&#xA;　　　　RETURN(缺省值)&#xA;END IF&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;decode(字段或字段的运算，值1，值2，值3）&lt;/p&gt;&#xA;&lt;p&gt;这个函数运行的结果是，当字段或字段的运算的值等于值1时，该函数返回值2，否则返回值3&#xA;当然值1，值2，值3也可以是表达式，这个函数使得某些sql语句简单了许多&lt;/p&gt;&#xA;&lt;p&gt;使用方法：&#xA;1、比较大小&#xA;&lt;code&gt;select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值&lt;/code&gt;&#xA;sign()函数根据某个值是0、正数还是负数，分别返回0、1、-1&#xA;例如：&#xA;变量1=10，变量2=20&#xA;则sign(变量1-变量2)返回-1，decode解码结果为“变量1”，达到了取较小值的目的。&lt;/p&gt;&#xA;&lt;p&gt;2、此函数用在SQL语句中，功能介绍如下：&lt;/p&gt;&#xA;&lt;p&gt;Decode函数与一系列嵌套的 IF-THEN-ELSE语句相似。base_exp与compare1,compare2等等依次进行比较。如果base_exp和 第i 个compare项匹配，就返回第i 个对应的value 。如果base_exp与任何的compare值都不匹配，则返回default。每个compare值顺次求值，如果发现一个匹配，则剩下的compare值（如果还有的话）就都不再求值。一个为NULL的base_exp被认为和NULL compare值等价。如果需要的话，每一个compare值都被转换成和第一个compare 值相同的数据类型，这个数据类型也是返回值的类型。&lt;/p&gt;&#xA;&lt;p&gt;Decode函数在实际开发中非常的有用&lt;/p&gt;&#xA;&lt;p&gt;结合Lpad函数，如何使主键的值自动加1并在前面补0&#xA;&lt;code&gt;select LPAD(decode(count(记录编号),0,1,max(to_number(记录编号)+1)),14,&#39;0&#39;) 记录编号 from tetdmis&lt;/code&gt;&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;eg:&#xA;&#xA;select decode(dir,1,0,1) from a1_interval&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;dir 的值是1变为0，是0则变为1&lt;/p&gt;&#xA;&lt;p&gt;比如我要查询某班男生和女生的数量分别是多少?&lt;/p&gt;&#xA;&lt;p&gt;通常我们这么写:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;select count(*) from 表 where 性别 ＝ 男；&#xA;&#xA;select count(*) from 表 where 性别 ＝ 女；&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;要想显示到一起还要union一下，太麻烦了&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
