?
? ????? PHP ??? ???? ??? ?? ??
本節(jié)描述了用于檢查和操作字符串?dāng)?shù)值的函數(shù)和操作符。 在這個環(huán)境中的字符串包括所有character, character varying和text類型的值。 除非另外說明,所有下面列出的函數(shù)都可以處理這些類型, 不過要小心的是,在使用character類型的時候,需要注意自動填充的潛在影響。 通常這里描述的函數(shù)也能用于非字符串類型,我們只要先把那些數(shù)據(jù)轉(zhuǎn)化為字符串表現(xiàn)形式就可 以了。有些函數(shù)還可以處理位串類型。
SQL定義了一些字符串函數(shù),它們有指定的語法(用特定的關(guān)鍵字而不是逗號來分隔參數(shù))。 詳情參閱Table 9-5,PostgreSQL也提供了這些函數(shù)的版本, 它們使用普通的函數(shù)調(diào)用語法。 (參閱Table 9-6)。
Note: 在PostgreSQL8.3之前, 這些函數(shù)將默默接受一些非字符串?dāng)?shù)據(jù)類型的值, 由于存在從這些數(shù)據(jù)類型到text的隱式強(qiáng)制轉(zhuǎn)換,轉(zhuǎn)換后的它們經(jīng)常發(fā)生意外的行為,因此刪除了隱式強(qiáng)制轉(zhuǎn)換。 然而,字符串連接操作(||)仍接受非字符串輸入,只要至少有一個輸入一個字符串類型, 如顯示在Table 9-5。對于其它情況下,如果你需要重復(fù)以前的行為, 插入一個明確的強(qiáng)制轉(zhuǎn)換到text。
Table 9-5. SQL字符串函數(shù)和操作符
函數(shù) | 返回類型 | 描述 | 示例 | 結(jié)果 |
---|---|---|---|---|
string|| string | text | 字符串連接 | 'Post' || 'greSQL' | PostgreSQL |
string|| non-string 或 non-string|| string | text | 一個帶有非字符串輸入的字符串串聯(lián) | 'Value: ' || 42 | Value: 42 |
bit_length(string) |
int | 字符串的位 | bit_length('jose') | 32 |
char_length(string) orcharacter_length(string)
|
int | 字符串中的字符個數(shù) | char_length('jose') | 4 |
lower(string) |
text | 把字符串轉(zhuǎn)化為小寫 | lower('TOM') | tom |
octet_length(string) |
int | 字符串中的字節(jié)數(shù) | octet_length('jose') | 4 |
overlay(stringplacingstringfromint[forint]) |
text | 替換子字符串 | overlay('Txxxxas' placing 'hom' from 2 for 4) | Thomas |
position(substringinstring) |
int | 指定子字符串的位置 | position('om' in 'Thomas') | 3 |
substring(string[fromint][forint]) |
text | 截取子字符串 | substring('Thomas' from 2 for 3) | hom |
substring(stringfrompattern) |
text | 截取匹配POSIX正則表達(dá)式的子字符串。參閱Section 9.7獲取更多關(guān)于模式匹配的信息。 | substring('Thomas' from '...$') | mas |
substring(stringfrompatternforescape) |
text | 截取匹配SQL正則表達(dá)式的子字符串。參閱Section 9.7獲取更多關(guān)于模式匹配的信息。 | substring('Thomas' from '%#"o_a#"_' for '#') | oma |
trim([leading | trailing | both]
[characters]from
string)
|
text | 從字符串string的開頭/結(jié)尾/兩邊刪除只包含characters中字符 (缺省是空白)的最長的字符串 | trim(both 'x' from 'xTomxx') | Tom |
upper(string) |
text | 把字符串轉(zhuǎn)化為大寫 | upper('tom') | TOM |
還有額外的字符串操作函數(shù)可以用,它們在Table 9-6列出。 它們有些在內(nèi)部用于實(shí)現(xiàn)Table 9-5列出的SQL標(biāo)準(zhǔn)字符串函數(shù)。
Table 9-6. 其它字符串函數(shù)
函數(shù) | 返回類型 | 描述 | 示例 | 結(jié)果 |
---|---|---|---|---|
ascii(string) |
int | 獲取參數(shù)中第一個字符的ASCII編碼值。對于UTF8返回字符的寬字節(jié)編碼值。 對于其它的多字節(jié)編碼,參數(shù)必須是一個ASCII字符。 | ascii('x') | 120 |
btrim(stringtext
[,characterstext]) |
text | 從string開頭和結(jié)尾刪除只包含characters中字符(缺省是空白)的最長字符串。 | btrim('xyxtrimyyx', 'xy') | trim |
chr(int) |
text | 給定編碼的字符。對于UTF8這個參數(shù)作為寬字節(jié)代碼處理。對于其它的多字節(jié)編碼, 這個參數(shù)必須指定一個ASCII字符,因?yàn)閠ext數(shù)據(jù)類型無法存儲NULL數(shù)據(jù)字節(jié),不能將NULL(0)作為字符參數(shù)。 | chr(65) | A |
convert(stringbytea,
src_encodingname,
dest_encodingname)
|
bytea | 將字符串轉(zhuǎn)化為dest_encoding格式的字符串。 src_encoding表示原始的編碼格式。在這種編碼格式中string必須是有效的。 用CREATE CONVERSION定義轉(zhuǎn)換。這也有些預(yù)定義的轉(zhuǎn)換。 參閱Table 9-7顯示可用的轉(zhuǎn)換。 | convert('text_in_utf8', 'UTF8', 'LATIN1') | text_in_utf8represented in Latin-1 encoding (ISO 8859-1) |
convert_from(stringbytea,
src_encodingname)
|
text | 將字符串轉(zhuǎn)化為數(shù)據(jù)庫編碼格式的,src_encoding表示原始的編碼格式,這種編碼格式中,string必須是有效的。 | convert_from('text_in_utf8', 'UTF8') | text_in_utf8represented in the current database encoding |
convert_to(stringtext,
dest_encodingname)
|
bytea | 將字符串轉(zhuǎn)化為dest_encoding編碼格式。 | convert_to('some text', 'UTF8') | some textrepresented in the UTF8 encoding |
decode(stringtext,
typetext)
|
bytea | 把前邊用encode 編碼的string里面的二進(jìn)制數(shù)據(jù)解碼。參數(shù)類型和encode 相同。
|
decode('MTIzAAE=', 'base64') | 123\000\001 |
encode(databytea,
typetext)
|
text | 把二進(jìn)制數(shù)據(jù)編碼為只包含ASCII形式的數(shù)據(jù)。支持的類型有base64,hex,escape。 Escape輸出空字節(jié)用\000和雙反斜杠. | encode(E'123\\000\\001', 'base64') | MTIzAAE= |
initcap(string) |
text | 把每個單詞的第一個子母轉(zhuǎn)為大寫,其它的保留小寫。單詞是一系列字母數(shù)字組成的字符,用非字母數(shù)字分隔。 | initcap('hi THOMAS') | Hi Thomas |
length(string) |
int | string中字符數(shù) | length('jose') | 4 |
length(stringbytea,
encodingname) |
int | 指定encoding編碼格式的string的字符數(shù)。 在這個編碼格式中,string必須是有效的。 | length('jose', 'UTF8') | 4 |
lpad(stringtext,
lengthint
[,filltext])
|
text | 通過填充字符fill(缺省時為空白),把string填充為length長度。 如果string已經(jīng)比length長則將其尾部截斷。 | lpad('hi', 5, 'xy') | xyxhi |
ltrim(stringtext
[,characterstext])
|
text | 從字符串string的開頭刪除只包含characters中字符(缺省為空白)的最長的字符串。 | ltrim('zzzytrim', 'xyz') | trim |
md5(string) |
text | 計算string的MD5散列,以十六進(jìn)制返回結(jié)果。 | md5('abc') | 900150983cd24fb0 d6963f7d28e17f72 |
pg_client_encoding() |
name | 當(dāng)前客戶端編碼名稱 | pg_client_encoding() | SQL_ASCII |
quote_ident(stringtext) |
text | 返回適用于SQL語句的標(biāo)識符形式(使用適當(dāng)?shù)囊栠M(jìn)行界定)。只有在必要的時候才會添加引號(字符串包含非標(biāo)識符字符或者會轉(zhuǎn)換大小寫的字符)。 里面的單引號和反斜杠會處理為雙份。也可以參閱Example 39-1。 | quote_ident('Foo bar') | "Foo bar" |
quote_literal(stringtext) |
text | 返回適用于在SQL語句里當(dāng)作文本使用的形式。嵌入的引號和反斜杠處理為雙份。注意quote_literal 當(dāng)空值輸入時返回空值;如果參數(shù)可能有空值,
用這個quote_nullable 往往更合適。參閱Example 39-1。
|
quote_literal('O\'Reilly') | 'O''Reilly' |
quote_literal(valueanyelement) |
text | 將給定的值強(qiáng)制轉(zhuǎn)換為text,加上引號作為文本。里面的單引號和反斜杠會處理為雙份。 | quote_literal(42.5) | '42.5' |
quote_nullable(stringtext) |
text | 返回指定字符串適當(dāng)?shù)囊茫鳛橐粭lSQL語句的字符串;或者,如果參數(shù)為空的話,返回NULL。 里面的單引號和反斜杠會處理為雙份。參閱Example 39-1。 | quote_nullable(NULL) | NULL |
quote_nullable(valueanyelement) |
text | 將給定的參數(shù)值轉(zhuǎn)化為text,加上引號作為文本;或者,如果參數(shù)為空的話,返回NULL。里面的單引號和反斜杠會處理為雙份。 | quote_nullable(42.5) | '42.5' |
regexp_matches(stringtext,patterntext[,flagstext]) |
setof text[] | 返回string中所有匹配POSIX正則表達(dá)式的子字符串。參閱Section 9.7.3獲得更多模式匹配的信息。 | regexp_matches('foobarbequebaz', '(bar)(beque)') | {bar,beque} |
regexp_replace(stringtext,patterntext,replacementtext[,flagstext]) |
text | 替換匹配POSIX正則表達(dá)式的子字符串。參閱Section 9.7.3以獲取更多模式匹配的信息。 | regexp_replace('Thomas', '.[mN]a.', 'M') | ThM |
regexp_split_to_array(stringtext,patterntext[,flagstext]) |
text[] | 用POSIX正則表達(dá)式作為分隔符,分隔string。參閱Section 9.7.3以獲取更多模式匹配的信息。 | regexp_split_to_array('hello world', E'\\s+') | {hello,world} |
regexp_split_to_table(stringtext,patterntext[,flagstext]) |
setof text | 用POSIX正則表達(dá)式作為分隔符,分隔string。參閱Section 9.7.3以獲取更多模式匹配的信息。 | regexp_split_to_table('hello world', E'\\s+') |
hello world (2 rows) |
repeat(stringtext,numberint) |
text | 將string重復(fù)number次 | repeat('Pg', 4) | PgPgPgPg |
replace(stringtext,
fromtext,
totext) |
text | Replace all occurrences instringof substring fromwith substringto 將字符串string里出現(xiàn)地所有子字符串from替換成子字符串to。 | replace('abcdefabcdef', 'cd', 'XX') | abXXefabXXef |
rpad(stringtext,
lengthint
[,filltext])
|
text | 使用填充字符fill(缺省時為空白),把string填充到length長度。 如果string已經(jīng)比length長則將其從尾部截斷。 | rpad('hi', 5, 'xy') | hixyx |
rtrim(stringtext
[,characterstext])
|
text | 從字符串string的結(jié)尾刪除只包含characters中字符(缺省是一個空白符)的最長的字符串。 | rtrim('trimxxxx', 'x') | trim |
split_part(stringtext,
delimitertext,
fieldint) |
text | 用delimiter分隔string返回給定的字段(1為基)。 | split_part('abc~@~def~@~ghi', '~@~', 2) | def |
strpos(string,substring) |
int | 指定的子字符串的位置。(同position(substringinstring),不過參數(shù)順序相反。) | strpos('high', 'ig') | 2 |
substr(string,from[,count]) |
text | 截取子字符串。(同substring(stringfromfromforcount))) | substr('alphabet', 3, 2) | ph |
to_ascii(stringtext
[,encodingtext]) |
text | 將string從其它編碼轉(zhuǎn)換為ASCII(僅支持LATIN1,LATIN2,LATIN9,WIN1250編碼)。 | to_ascii('Karel') | Karel |
to_hex(numberint
orbigint) |
text | 將number轉(zhuǎn)換成十六進(jìn)制表現(xiàn)形式 | to_hex(2147483647) | 7fffffff |
translate(stringtext,
fromtext,
totext)
|
text | 將string中任何匹配from字符集中的字符轉(zhuǎn)化為對應(yīng)的在to字符集的字符。 | translate('12345', '14', 'ax') | a23x5 |
See also the aggregate functionstring_agg
in
Section 9.18.
Table 9-7. 內(nèi)置的轉(zhuǎn)換
轉(zhuǎn)換名 [a] | 源編碼 | 目的編碼 |
---|---|---|
ascii_to_mic | SQL_ASCII | MULE_INTERNAL |
ascii_to_utf8 | SQL_ASCII | UTF8 |
big5_to_euc_tw | BIG5 | EUC_TW |
big5_to_mic | BIG5 | MULE_INTERNAL |
big5_to_utf8 | BIG5 | UTF8 |
euc_cn_to_mic | EUC_CN | MULE_INTERNAL |
euc_cn_to_utf8 | EUC_CN | UTF8 |
euc_jp_to_mic | EUC_JP | MULE_INTERNAL |
euc_jp_to_sjis | EUC_JP | SJIS |
euc_jp_to_utf8 | EUC_JP | UTF8 |
euc_kr_to_mic | EUC_KR | MULE_INTERNAL |
euc_kr_to_utf8 | EUC_KR | UTF8 |
euc_tw_to_big5 | EUC_TW | BIG5 |
euc_tw_to_mic | EUC_TW | MULE_INTERNAL |
euc_tw_to_utf8 | EUC_TW | UTF8 |
gb18030_to_utf8 | GB18030 | UTF8 |
gbk_to_utf8 | GBK | UTF8 |
iso_8859_10_to_utf8 | LATIN6 | UTF8 |
iso_8859_13_to_utf8 | LATIN7 | UTF8 |
iso_8859_14_to_utf8 | LATIN8 | UTF8 |
iso_8859_15_to_utf8 | LATIN9 | UTF8 |
iso_8859_16_to_utf8 | LATIN10 | UTF8 |
iso_8859_1_to_mic | LATIN1 | MULE_INTERNAL |
iso_8859_1_to_utf8 | LATIN1 | UTF8 |
iso_8859_2_to_mic | LATIN2 | MULE_INTERNAL |
iso_8859_2_to_utf8 | LATIN2 | UTF8 |
iso_8859_2_to_windows_1250 | LATIN2 | WIN1250 |
iso_8859_3_to_mic | LATIN3 | MULE_INTERNAL |
iso_8859_3_to_utf8 | LATIN3 | UTF8 |
iso_8859_4_to_mic | LATIN4 | MULE_INTERNAL |
iso_8859_4_to_utf8 | LATIN4 | UTF8 |
iso_8859_5_to_koi8_r | ISO_8859_5 | KOI8R |
iso_8859_5_to_mic | ISO_8859_5 | MULE_INTERNAL |
iso_8859_5_to_utf8 | ISO_8859_5 | UTF8 |
iso_8859_5_to_windows_1251 | ISO_8859_5 | WIN1251 |
iso_8859_5_to_windows_866 | ISO_8859_5 | WIN866 |
iso_8859_6_to_utf8 | ISO_8859_6 | UTF8 |
iso_8859_7_to_utf8 | ISO_8859_7 | UTF8 |
iso_8859_8_to_utf8 | ISO_8859_8 | UTF8 |
iso_8859_9_to_utf8 | LATIN5 | UTF8 |
johab_to_utf8 | JOHAB | UTF8 |
koi8_r_to_iso_8859_5 | KOI8R | ISO_8859_5 |
koi8_r_to_mic | KOI8R | MULE_INTERNAL |
koi8_r_to_utf8 | KOI8R | UTF8 |
koi8_r_to_windows_1251 | KOI8R | WIN1251 |
koi8_r_to_windows_866 | KOI8R | WIN866 |
koi8_u_to_utf8 | KOI8U | UTF8 |
mic_to_ascii | MULE_INTERNAL | SQL_ASCII |
mic_to_big5 | MULE_INTERNAL | BIG5 |
mic_to_euc_cn | MULE_INTERNAL | EUC_CN |
mic_to_euc_jp | MULE_INTERNAL | EUC_JP |
mic_to_euc_kr | MULE_INTERNAL | EUC_KR |
mic_to_euc_tw | MULE_INTERNAL | EUC_TW |
mic_to_iso_8859_1 | MULE_INTERNAL | LATIN1 |
mic_to_iso_8859_2 | MULE_INTERNAL | LATIN2 |
mic_to_iso_8859_3 | MULE_INTERNAL | LATIN3 |
mic_to_iso_8859_4 | MULE_INTERNAL | LATIN4 |
mic_to_iso_8859_5 | MULE_INTERNAL | ISO_8859_5 |
mic_to_koi8_r | MULE_INTERNAL | KOI8R |
mic_to_sjis | MULE_INTERNAL | SJIS |
mic_to_windows_1250 | MULE_INTERNAL | WIN1250 |
mic_to_windows_1251 | MULE_INTERNAL | WIN1251 |
mic_to_windows_866 | MULE_INTERNAL | WIN866 |
sjis_to_euc_jp | SJIS | EUC_JP |
sjis_to_mic | SJIS | MULE_INTERNAL |
sjis_to_utf8 | SJIS | UTF8 |
tcvn_to_utf8 | WIN1258 | UTF8 |
uhc_to_utf8 | UHC | UTF8 |
utf8_to_ascii | UTF8 | SQL_ASCII |
utf8_to_big5 | UTF8 | BIG5 |
utf8_to_euc_cn | UTF8 | EUC_CN |
utf8_to_euc_jp | UTF8 | EUC_JP |
utf8_to_euc_kr | UTF8 | EUC_KR |
utf8_to_euc_tw | UTF8 | EUC_TW |
utf8_to_gb18030 | UTF8 | GB18030 |
utf8_to_gbk | UTF8 | GBK |
utf8_to_iso_8859_1 | UTF8 | LATIN1 |
utf8_to_iso_8859_10 | UTF8 | LATIN6 |
utf8_to_iso_8859_13 | UTF8 | LATIN7 |
utf8_to_iso_8859_14 | UTF8 | LATIN8 |
utf8_to_iso_8859_15 | UTF8 | LATIN9 |
utf8_to_iso_8859_16 | UTF8 | LATIN10 |
utf8_to_iso_8859_2 | UTF8 | LATIN2 |
utf8_to_iso_8859_3 | UTF8 | LATIN3 |
utf8_to_iso_8859_4 | UTF8 | LATIN4 |
utf8_to_iso_8859_5 | UTF8 | ISO_8859_5 |
utf8_to_iso_8859_6 | UTF8 | ISO_8859_6 |
utf8_to_iso_8859_7 | UTF8 | ISO_8859_7 |
utf8_to_iso_8859_8 | UTF8 | ISO_8859_8 |
utf8_to_iso_8859_9 | UTF8 | LATIN5 |
utf8_to_johab | UTF8 | JOHAB |
utf8_to_koi8_r | UTF8 | KOI8R |
utf8_to_koi8_u | UTF8 | KOI8U |
utf8_to_sjis | UTF8 | SJIS |
utf8_to_tcvn | UTF8 | WIN1258 |
utf8_to_uhc | UTF8 | UHC |
utf8_to_windows_1250 | UTF8 | WIN1250 |
utf8_to_windows_1251 | UTF8 | WIN1251 |
utf8_to_windows_1252 | UTF8 | WIN1252 |
utf8_to_windows_1253 | UTF8 | WIN1253 |
utf8_to_windows_1254 | UTF8 | WIN1254 |
utf8_to_windows_1255 | UTF8 | WIN1255 |
utf8_to_windows_1256 | UTF8 | WIN1256 |
utf8_to_windows_1257 | UTF8 | WIN1257 |
utf8_to_windows_866 | UTF8 | WIN866 |
utf8_to_windows_874 | UTF8 | WIN874 |
windows_1250_to_iso_8859_2 | WIN1250 | LATIN2 |
windows_1250_to_mic | WIN1250 | MULE_INTERNAL |
windows_1250_to_utf8 | WIN1250 | UTF8 |
windows_1251_to_iso_8859_5 | WIN1251 | ISO_8859_5 |
windows_1251_to_koi8_r | WIN1251 | KOI8R |
windows_1251_to_mic | WIN1251 | MULE_INTERNAL |
windows_1251_to_utf8 | WIN1251 | UTF8 |
windows_1251_to_windows_866 | WIN1251 | WIN866 |
windows_1252_to_utf8 | WIN1252 | UTF8 |
windows_1256_to_utf8 | WIN1256 | UTF8 |
windows_866_to_iso_8859_5 | WIN866 | ISO_8859_5 |
windows_866_to_koi8_r | WIN866 | KOI8R |
windows_866_to_mic | WIN866 | MULE_INTERNAL |
windows_866_to_utf8 | WIN866 | UTF8 |
windows_866_to_windows_1251 | WIN866 | WIN |
windows_874_to_utf8 | WIN874 | UTF8 |
euc_jis_2004_to_utf8 | EUC_JIS_2004 | UTF8 |
ut8_to_euc_jis_2004 | UTF8 | EUC_JIS_2004 |
shift_jis_2004_to_utf8 | SHIFT_JIS_2004 | UTF8 |
ut8_to_shift_jis_2004 | UTF8 | SHIFT_JIS_2004 |
euc_jis_2004_to_shift_jis_2004 | EUC_JIS_2004 | SHIFT_JIS_2004 |
shift_jis_2004_to_euc_jis_2004 | SHIFT_JIS_2004 | EUC_JIS_2004 |
Notes: a. 轉(zhuǎn)換名遵循一個標(biāo)準(zhǔn)的命名模式:將源編碼的正式名稱中所有非字母數(shù)字字符用下劃線替換,后面跟著_to_, 然后再跟著經(jīng)過同樣處理的目標(biāo)編碼的名字。因此這些名字可能和客戶的編碼名字不同。 |