隨筆-26  評論-13  文章-46  trackbacks-0

          In Oracle/PLSQL, the NVL function lets you substitute a value when a null value is encountered.

          The syntax for the NVL function is:

          NVL( string1, replace_with )

          string1 is the string to test for a null value.

          replace_with is the value returned if string1 is null.


          Example #1:

          select NVL(supplier_city, 'n/a')
          from suppliers;

          The SQL statement above would return 'n/a' if the supplier_city field contained a null value. Otherwise, it would return the supplier_city value.


          Example #2:

          select supplier_id,
          NVL(supplier_desc, supplier_name)
          from suppliers;

          This SQL statement would return the supplier_name field if the supplier_desc contained a null value. Otherwise, it would return the supplier_desc.


          Example #3:

          select NVL(commission, 0)
          from sales;

          This SQL statement would return 0 if the commission field contained a null value. Otherwise, it would return the commission field.


          Frequently Asked Questions


          Question:? I tried to use the NVL function through VB to access Oracle DB.

          To be precise,

          select NVL(DIstinct (emp_name),'AAA'),................ from.................

          I got an oracle error when I use distinct clause with NVL, but when I remove distinct it works fine.

          Answer:? It is possible to the use the DISTINCT clause with the NVL function. However, the DISTINCT must come before the use of the NVL function. For example:

          select distinct NVL(emp_name, 'AAA')
          from employees;

          Hope this helps!


          Question:? Is it possible to use the NVL function with more than one column with the same function call?? To be clear, if i need to apply this NVL function to more than one column like this:

          NVL(column1;column2 ...... , here is the default value for all )

          Answer:? You will need to make separate NVL function calls for each column. For example:

          select NVL(table_name, 'not found'), NVL(owner, 'not found')
          from all_tables;

          posted on 2006-04-05 13:38 似水流年 閱讀(647) 評論(0)  編輯  收藏 所屬分類: Oracle
          主站蜘蛛池模板: 盐城市| 奎屯市| 灵璧县| 松江区| 遵义市| 英吉沙县| 筠连县| 舞阳县| 松滋市| 南康市| 商河县| 海淀区| 香格里拉县| 湟源县| 保靖县| 永修县| 木里| 龙里县| 永丰县| 德阳市| 四平市| 宜良县| 元氏县| 渑池县| 延长县| 行唐县| 武义县| 驻马店市| 古浪县| 抚顺市| 宜昌市| 安丘市| 常宁市| 全州县| 潮州市| 舒兰市| 海城市| 竹北市| 临泽县| 舒城县| 桓仁|