รวมคำสั่งและการใช้งาน Function จัดการ String (ข้อความ อักขระ) ใน PLSQL ORACLE

SQL String Functions - ฟังก์ชั่นจัดการข้อความ ตัวอักขระ ใน SQL
จากที่ได้แนะนำบทความ รูปแบบSyntax และตัวอย่างการใช้งานคำสั่ง Oracle/PLSQL: To_Char Function และ รูปแบบSyntax และตัวอย่างการใช้งานคำสั่ง Oracle/PLSQL: To_Date Function สำหรับใช้งานบน PL/SQL ORACLE กันไปแล้ว มาวันนี้ ผมจะมาแนะนำการใช้งานการใช้งาน Function จัดการ String (ข้อความ อักขระ) ใน PLSQL ORACLE กันนะครับ


These functions can be used within sql or pl/sql statements. For example:

โค๊ดตัวอย่าง:
   SELECT UPPER(FNAME)
    FROM EMPLOYEE;
    V_UpperName := upper(v_lowername);

Functions Returning Strings

    * UPPER(string), LOWER(string), INITCAP(string) - เปลี่ยนตัวพิมพ์เล็ก พิมพ์ใหญ่
      Returns string in all upper case, all lower case, first letter of each word capitalized, respectively.

    * LPAD(string, length, pad), RPAD(string, length, pad) - ตัวข้อความ ตัดคำ
      Returns string padded on left or right to length characters using the pad string as padding. Pad may be omitted: defaults to single space.

    * LTRIM(string,trimlist), RTRIM(string,trimlist) - ลบ/ตัดข้อความที่เกิดจากการเคาะ Space ช่องว่าง
      Returns string with the leftmost or rightmost characters that match the characters in trimlist removed. Trimlist may be omitted: defaults to single space.

    * REPLACE(string,target,replacement) - แทนที่คำหรือข้อความ
      Returns string with all occurances of target replaced with replacement. If replacement is omitted, occurances are deleted.

    * SUBSTR(string,pos,len) - ตัวข้อความ ตัดคำ
      Returns the substring of string which begins at pos and is len characters long. If pos is negative, pos is counted from the end of string. The first position in string is 1 not 0.

    * TRANSLATE(string,fromlist,tolist) - แทนที่คำ ข้อความ ตัวอักษรแบบมีเงื่อนไข
      Returns string with each character in the fromlist replaced with the corresponding character in the tolist.

    * TO_CHAR(number,'format') - แปลงตัวเลขเป็นข้อความ
      formats the number according to the format string (which must be within single quotes.
      Some Numeric Formats 9990.90    show 4 digits to the left of the decimal point and 2 to the right. If the value is zero, show 0.00. If the number is too big, show '#######'
      $9999    show a '$' before each number in the column.
      999,999,999.99    use commas and decimals according to the pattern shown.
       
Character functions Returning Numeric Values

    * INSTR(string,target,start,nth) - หาตำแหน่งตัวเลขหรือตัวอักษรในข้อความที่สนใจ
      Returns the position in string, starting the search at start, of the nth occurance of target. Start and nth may be omitted and default to 1. If target is not found, 0 is returned.

    * LENGTH(string) - หาความยาวคำหรือข้อความ
      Returns length of string in characters.

Miscellaneous Useful Functions

    * NVL(value,substitute)
      if value is null, this function is equal to substitute, otherwise it is equal to value. Value can be any Oracle datatype; substitute can be a literal, another column, or an expression but must result in the same datatype as value.

โค๊ดตัวอย่าง:
-- ordinarily SUM ignores nulls:
SQL> select sum(hours) from works_on;
SUM(HOURS)
----------
       296
-- but when everything summed is null, it returns null
SQL> select sum(hours) from works_on
  2  where hours is null;
SUM(HOURS)
----------
-- to make nulls behave like 0, use NVL:
SQL> select sum(nvl(hours,0)) from works_on
  2  where hours is null;
SUM(NVL(HOURS,0))
-----------------
                0

Checking Out Functions in SQLPlus

You can use SQLplus to see what the output from a function will be. The following use the tiny table named DUAL which has one row and one column and exists just for this purpose.

โค๊ดตัวอย่าง:
SQL> select to_char(0,'990.90') from dual;
TO_CHAR
-------
   0.00
SQL> select to_char(123456789,'990.90') from dual;
TO_CHAR
-------
#######
เป็นยังไงกันบ้างครับกับบทความรวมคำสั่งและการใช้งาน Function จัดการ String (ข้อความ อักขระ) ใน PLSQL ORACLE ลองนำไปประยุกต์ใช้กันดูนะครับ ติดปัญหาหรือสงสัย โพสต์คอมเม้นท์ สอบถามได้ที่ใต้โพสต์นี้ได้เลยครับ :)

ไม่มีความคิดเห็น:

แสดงความคิดเห็น