程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> DB2判斷漢字長度、對數字字符進行判斷

DB2判斷漢字長度、對數字字符進行判斷

編輯:DB2教程

DB2判斷漢字長度、對數字字符進行判斷   項目有個模塊需要寫校驗規則,而規則中有個需求是這麼描述的   XXX要求大於三個字符或兩個漢字,把不滿足的都過濾出來。    對於字符判斷長度的想必大家都知道用length()函數,但是對於漢字呢?這是個問題。   通過用length函數測出一個漢字的長度是3,即一個漢字等於3個字節長度。(BTW:項目用的DB2設置的編碼格式是UTF-8)   通過查看DB2文檔,發現length函數是可以通過編碼格式不同來實現輸出不同的長度,具體如下:   LENGTH scalar function  >>-LENGTH--(--expression--+--------------------+--)------------><                           '-,--+-CODEUNITS16-+-'                                      +-CODEUNITS32-+                                        '-OCTETS------'         expression   An expression that returns a value that is a built-in data type. If expression can be null, the result can be null; if expression is null, the result is the null value.   CODEUNITS16, CODEUNITS32, or OCTETS   Specifies the string unit of the result. CODEUNITS16 specifies that the result is to be expressed in 16-bit UTF-16 code units. CODEUNITS32 specifies that the result is to be expressed in 32-bit UTF-32 code units. OCTETS specifies that the result is to be expressed in bytes.   最後過濾語句為   1 ( length(r.ITEM, CODEUNITS32)<2 or length(r.ITEM)<3)   對於數字字符的判斷,用到的函數是translate()   TRANSLATE scalar function character string expression:  >>-TRANSLATE--(--char-string-exp-------------------------------->   >--+-----------------------------------------------------------+-->    |                                       .-,--' '----------. |       '-,--to-string-exp--,--from-string-exp--+-----------------+-'                                               '-,--pad-char-exp-'        >--)----------------------------------------------------------->< The TRANSLATE function returns a value in which one or more characters in a string expression might have been converted to other characters.   The function converts all the characters in char-string-exp or graphic-string-exp that also occur in from-string-exp to the corresponding characters in to-string-exp or, if no corresponding characters exist, to the pad character specified by pad-char-exp.   char-string-exp or graphic-string-exp   Specifies a string that is to be converted. The expression must return a value that is a built-in CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, numeric, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it is implicitly cast to VARCHAR before evaluating the function.   to-string-exp   Specifies a string of characters to which certain characters in char-string-exp will be converted.The expression must return a value that is a built-in CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, numeric, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it is implicitly cast to VARCHAR before evaluating the function.If a value for to-string-exp is not specified, and the data type is not graphic, all characters in char-string-exp will be in monocase; that is, the characters a-z will be converted to the characters A-Z, and other characters will be converted to their uppercase equivalents, if they exist. For example, in code page 850, é maps to É, but ÿ is not mapped, because code page 850 does not include Ÿ. If the code point length of the result character is not the same as the code point length of the source character, the source character is not converted.   from-string-exp Specifies a string of characters which, if found in char-string-exp, will be converted to the corresponding character in to-string-exp.   The expression must return a value that is a built-in CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, numeric, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it is implicitly cast to VARCHAR before evaluating the function.If from-string-exp contains duplicate characters, the first one found will be used, and the duplicates will be ignored. If to-string-exp is longer than from-string-exp, the surplus characters will be ignored. If to-string-exp is specified, from-string-exp must also be specified.   pad-char-exp Specifies a single character that will be used to pad to-string-exp if to-string-exp is shorter than from-string-exp. The expression must return a value that is a built-in CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, numeric, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it is implicitly cast to VARCHAR before evaluating the function. The value must have a length attribute of zero or one. If a zero-length string is specified, characters in the from-string-exp with no corresponding character in the to-string-exp are removed from char-string-exp or graphic-string-exp. If a value is not specified a single-byte blank character is assumed.   具體實現語句如下: 1 length(trim(translate(r.ITEM,'','0123456789')))=0 這條sql語句簡單說明,當ITEM字段的中包含0~9中的任一數字時,就替換為空字符,當全部為數字時,就全部替換為空字符了,通過trim()函數去空格然後length函數一測量,連渣都不剩了.為0.此時就可以判定為全部是數字字符了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved