全部產品
Search
文件中心

PolarDB:字串函數

更新時間:Jul 06, 2024

在這個章節中介紹了用於檢測和操作字串值的函數和操作符。

字串包括CHAR、VARCHAR2和CLOB類型的值。除非另外提示,在下面所列出的函數可以在所有這些類型的值上工作,但是需要注意的是當使用CHAR類型的字串時,會出現自動在字串後面添加空格的情況。一般來說,通過將資料轉換成字串類型的值,在這裡介紹的函數也可以在非字串類型的值上工作。

函數傳回型別描述樣本結果
string || stringCLOBString concatenation'Polar' || 'DB'PolarDB
CONCAT(string, string)CLOBString concatenationCONCAT('a' || 'b')ab
HEXTORAW(varchar2)RAWConverts a VARCHAR2 value to a RAW valueHEXTORAW('303132')'012'
RAWTOHEX(raw)VARCHAR2Converts a RAW value to a HEXADECIMAL valueRAWTOHEX('012')'303132'
INSTR(string, set, [ start [, occurrence ] ])INTEGERFinds the location of a set of characters in a string, starting at position start in the string, string, and looking for the first, second, third and so on occurrences of the set. Returns 0 if the set is not found.INSTR('PETER PIPER PICKED a PECK of PICKLED PEPPERS','PI',1,3)30
INSTRB(string, set, start)INTEGERReturns the position of the set within the string, beginning at start. Returns 0 if set is not found.INSTRB('PETER PIPER PICKED a PECK of PICKLED PEPPERS','PICK', 14)30
INSTRB(string, set, start, occurrence)INTEGERReturns the position of the specified occurrence of set within the string, beginning at start. Returns 0 if set is not found.INSTRB('PETER PIPER PICKED a PECK of PICKLED PEPPERS','PICK', 1, 2)30
LOWER(string)CLOBConvert string to lower caseLOWER('TOM')tom
SUBSTR(string, start [, count ])CLOBExtract substring starting from start and going for count characters. If count is not specified, the string is clipped from the start till the end.SUBSTR('This is a test',6,2)is
SUBSTRB(string, start [, count ])CLOBSame as SUBSTR except start and count are in number of bytes.SUBSTRB('abc',3) (assuming a double-byte character set)c
SUBSTR2(string, start[, count ])CLOBAlias for SUBSTR.SUBSTR2('This is atest',6,2)is
SUBSTR2(string, start [, count ])CLOBAlias for SUBSTRB.SUBSTR2('abc',3) (assuming a double-byte character set)c
SUBSTR4(string, start [, count ])CLOBAlias for SUBSTR.SUBSTR4('This is a test',6,2)is
SUBSTR4(string, start [, count ])CLOBAlias for SUBSTRB.SUBSTR4('abc',3) (assuming a double-byte character set)c
SUBSTRC(string, start [, count ])CLOBAlias for SUBSTR.SUBSTRC('This is a test',6,2)is
SUBSTRC(string, start [, count ])CLOBAlias for SUBSTRB.SUBSTRC('abc',3) (assuming a double-byte character set)c
TRIM([ LEADING | TRAILING | BOTH ] [ characters ] FROM string)CLOBRemove the longest string containing only the characters (a space by default) from the start/end/both ends of the string.TRIM(BOTH 'x' FROM 'xTomxx')Tom
LTRIM(string [, set])CLOBRemoves all the characters specified in set from the left of a given string. If set is not specified, a blank space is used as default.LTRIM('abcdefghi', 'abc')defghi
RTRIM(string [, set])CLOBRemoves all the characters specified in set from the right of a given string. If set is not specified, a blank space is used as default.RTRIM('abcdefghi', 'ghi')abcdef
UPPER(string)CLOBConvert string to upper caseUPPER('tom')TOM

在下面的表格中列出了另外一些允許使用的字串操作函數。它們中的一些是用於內部實現在比較子表中列出,符合SQL標準的字串函數。

函數傳回型別描述樣本結果
ASCII(string)INTEGERASCII code of the first byte of the argumentASCII('x')120
CHR(INTEGER)CLOBCharacter with the given ASCII codeCHR(65)A
DECODE(expr, exprla, exprlb [, expr2a, expr2b ]... [, default ])Same as argument types of expr1b, expr2b,..., defaultFinds first match of expr with expr1a, expr2a, etc. When match found, returns corresponding parameter pair, expr1b, expr2b, etc. If no match found, returns default. If no match found and default not specified, returns null.DECODE(3, 1,'One', 2,'Two', 3,'Three', 'Not found')Three
INITCAP(string)CLOBConvert the first letter of each word to uppercase and the rest to lowercase. Words are sequences of alphanumeric characters separated by non- alphanumeric characters.INITCAP('hi THOMAS')Hi Thomas
LENGTHINTEGERReturns the number of characters in a string value.LENGTH('Coted''Azur')11
LENGTHCINTEGERThis function is identical in functionality to LENGTH; the function name is supported for compatibility.LENGTHC ('Cote d''Azur')11
LENGTH2INTEGERThis function is identical in functionality to LENGTH; the function name is supported for compatibility.LENGTH2 ('Cote d''Azur')11
LENGTH4INTEGERThis function is identical in functionality to LENGTH; the function name is supported for compatibility.LENGTH4 ('Cote d''Azur')11
LENGTHBINTEGERReturns the number of bytes required to hold the given value.LENGTHB ('Cote d''Azur')12
LPAD(string, length INTEGER [, fill ])CLOBFill up string to size, length by prepending the characters, fill (a space by default). If string is already longer than length then it is truncated (on the right).LPAD('hi', 5, 'xy')xyxhi
REPLACE(string, search string [, replace string ]CLOBReplaces one value in a string with another. If you do not specify a value for replace string, the search_string value when found, is removed.REPLACE( 'GEORGE', 'GE', 'EG')EGOREG
RPAD(string, length INTEGER [, fill ])CLOBFill up string to size, length by appending the characters, fill (a space by default). If string is already longer than length then it is truncated.RPAD('hi', 5, 'xy')hixyx
TRANSLATE(string, from, to)CLOBAny character in string that matches a character in the from set is replaced by the corresponding character in the to set.TRANSLATE('12345', '14', 'ax')a23x5