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

                項目有個模塊須要寫校驗規則,而規則中有個需求是這麼描述的 html

XXX要求大於三個字符或兩個漢字,把不知足的都過濾出來。  sql

對於字符判斷長度的想必你們都知道用length()函數,可是對於漢字呢?這是個問題。 express

經過用length函數測出一個漢字的長度是3,即一個漢字等於3個字節長度。(BTW:項目用的DB2設置的編碼格式是UTF-8) app

經過查看DB2文檔,發現length函數是能夠經過編碼格式不一樣來實現輸出不一樣的長度,具體以下: 函數

LENGTH scalar function

Read syntax diagramSkip visual syntax diagram >>-LENGTH--(--expression--+--------------------+--)------------><
                          '-,--+-CODEUNITS16-+-'      
                               +-CODEUNITS32-+        
                               '-OCTETS------'        

expression ui

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 lua

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. spa

最後過濾語句爲 scala


( length(r.ITEM, CODEUNITS32)<2 or length(r.ITEM)<3)


對於數字字符的判斷,用到的函數是translate()

TRANSLATE scalar function

character string expression:

Read syntax diagramSkip visual syntax diagram >>-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-expThe 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.



具體實現語句以下:
length(trim(translate(r.ITEM,'','0123456789')))=0
這條sql語句簡單說明,當ITEM字段的中包含0~9中的任一數字時,就替換爲空字符,當所有爲數字時,就所有替換爲空字符了,經過trim()函數去空格而後length函數一測量,連渣都不剩了.爲0.此時就能夠斷定爲所有是數字字符了。
相關文章
相關標籤/搜索