常见SQL函数需要注意的细节
2012-12-23 18:27
169 查看
这是一位牛人让我们思考的问题,说实话当时真蒙了,函数虽然明白,但细化到这种程度,真的是叫不准啊,下面是几道比较典型的问题,和本人做的实验,不一定准确,而且测验的方法不只一种,请大家多多见谅,一起学习,共同进步!!!
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
知识共享~共同进步
转载请注明:
/article/9419020.html
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
知识共享~共同进步
转载请注明:
/article/9419020.html
相关文章推荐
- 编程中需要注意的细节
- 专业的程序员需要具备的思考能力:写一个程序需要注意多少细节问题
- 2009-07-27 22:09 使用hibernate需要注意的细节
- Android开发的一些需要注意的细节
- Android开发过程中需要注意的细节
- 登陆时获取上页请求地址时需要注意的细节
- CSS优化需要注意的15个细节【转】
- 应聘过程中需要注意的细节及如何规划职业发展生涯
- Jquery中的一些细节(一)--------------->Jquery加载调用多个函数(方法)需要注意的细节
- jQuery中需要注意的细节问题小结
- linux的安装步骤和安装过程中需要注意的细节图解
- 成为更好的Android开发者前,我们需要注意的31个细节
- 新站长交换友情链接需要注意那些细节
- android中的绘图需要注意的细节
- Android通知栏微技巧一些需要注意的小细节
- 曾经遇到的一个面试题,快速排序用链表实现,算法和以前的相似,需要注意一些细节处理
- 【学习OpenCV】编程笔记:一些需要注意的细节
- 当javaScript从入门到提高前需要注意的细节:闭包部分
- B2B网站推广中需要注意哪些细节
- JAVA socket通信中需要注意的几个小细节