版权声明:本文为博主原创文章,未经博主允许不得转载。
这是一位牛人让我们思考的问题,说实话当时真蒙了,函数虽然明白,但细化到这种程度,真的是叫不准啊,下面是几道比较典型的问题,和本人做的实验,不一定准确,而且测验的方法不只一种,请大家多多见谅,一起学习,共同进步!!!
1.在数字前如何补零
SQL> select lpad(‘123‘,10,‘0‘) from dual;
LPAD(‘123‘
----------
0000000123
SQL> select lpad(123,10,0) from dual;
LPAD(123,1
----------
0000000123
SQL> select lpad(‘123‘,10,‘0‘) from dual;
LPAD(‘123‘
----------
0000000123
2.如果用trim删除空格会不会同时删除掉字符串末尾的回车或者换行符
方法:这里面用到了chr函数。Chr函数功能:返回以数值表达式值为编码的字符。说明:函数返回值类型为string,其数值表达式取值范围为0~255.
Chr(10)代表回车
Chr(13)代表换行
Chr(32)代表空格
SQL> select length(‘Hello‘||chr(10)) from dual;
LENGTH(‘HELLO‘||CHR(10))
------------------------
6
SQL> select length(trim(‘Hello‘||chr(10))) from dual;
LENGTH(TRIM(‘HELLO‘||CHR(10)))
------------------------------
6
SQL> select length(‘Hello‘||chr(32)) from dual;
LENGTH(‘HELLO‘||CHR(32))
------------------------
6
SQL> select length(trim(‘Hello‘||chr(32))) from dual;
LENGTH(TRIM(‘HELLO‘||CHR(32)))
------------------------------
5
SQL> select length(‘Hello‘||chr(13)) from dual;
LENGTH(‘HELLO‘||CHR(13))
------------------------
6
SQL> select length(trim(‘Hello‘||chr(13))) from dual;
LENGTH(TRIM(‘HELLO‘||CHR(13)))
------------------------------
6
结论:由上面实验可以得出空格去除不了回车和换行符。
3.日期函数months_between的里面的两个值如果左边的值大于右边的值会是什么结果?
查阅了下官方文档,描述如下:
Syntax
Purpose
MONTHS_BETWEEN returns number of months between datesdate1 anddate2. The month and the last day of the month are defined by the parameterNLS_CALENDAR.If date1 is later thandate2, then the result is positive. Ifdate1 is earlier thandate2, then the result is negative. Ifdate1 anddate2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise Oracle Database calculates the fractional portion of the result based on a 31-day month and considers the difference in time componentsdate1 anddate2.
通过黄色部门文字描述,可以知道如果右边(date2)大于左边(date1)返回的number是负值。
实验:
SQL> select months_between
2 (to_date(‘02-02-2012‘,‘mm-dd-yyyy‘),
3 to_date(‘01-01-2012‘,‘mm-dd-yyyy‘)) "Months"
4 from dual;
Months
----------
1.03225806
SQL> select months_between
2 (to_date(‘2012-01-01‘,‘yyyy-mm-dd‘),
3 to_date(‘2012-02-02‘,‘yyyy-mm-dd‘)) "Months"
4 from dual;
Months
----------
-1.0322581
结论:如果date1小于date2值为负数
4.case…when函数如果取出的列值有Null的话该怎么判断?
SQL> select a,
2 case nvl(b,4) when 1 then 123
3 when 2 then 123
4 when 3 then 123
5 when 4 then 888
6 when 5 then 123
7 end as b
8 from t;
A B
---------- ----------
1 123
2 123
3 123
4 888
5 123
总结:我暂时能想到的方法就是用nvl函数转换成可见的数值或字符,我也默认不处理null得到的结果也为空,比如我不指定when 4,这样第4条也就为null了。
5.还有个就是与Null运算得出的结果也为空值。
SQL> select 5*null from dual;
5*NULL
elvis
2012.12.23
知识共享~共同进步
转载请注明:
http://blog.csdn.net/elvis_dataguru/article/details/8393947