常見SQL函數需要注意的細節

這是一位牛人讓我們思考的問題,說實話當時真蒙了,函數雖然明白,但細化到這種程度,真的是叫不準啊,下面是幾道比較典型的問題,和本人做的實驗,不一定準確,而且測驗的方法不只一種,請大家多多見諒,一起學習,共同進步!!!

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

發佈了54 篇原創文章 · 獲贊 42 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章