9.4. �ַ��������Ͳ�����

�������������ڼ��Ͳ����ַ�����ֵ�ĺ����Ͳ������� ����������е��ִ������������� character�� character varying���� text ��ֵ����������˵�������������г��ĺ��������Դ�����Щ���ͣ� ����ҪС�ĵ��ǣ���ʹ�� character ���͵�ʱ�� �����Զ�����DZ��Ӱ�졣ͨ�����������ĺ���Ҳ�����ڷ��ִ� ���ͣ�����ֻҪ�Ȱ���Щ����ת��Ϊ�ִ�������ʽ�Ϳ����ˡ� ��Щ���������Դ���λ�����͡�

SQL ������һЩ�ִ������� ������ָ�����﷨�������������� ij���ض��Ĺؼ��֣������Ƕ������ָ������� �������Table 9-5�� ��Щ����Ҳ�������ĺ�������˵��ʵ���ˡ� ������ Table 9-6����

Table 9-5. SQL �ִ������Ͳ�����

�����������������������
string || string text �ִ����� 'Post' || 'greSQL'PostgreSQL
bit_length(string)integer�ִ��������λ�ĸ���bit_length('jose')32
char_length(string) �� character_length(string)integer �ִ��е��ַ����� char_length('jose')4
convert(string using conversion_name)text ʹ��ָ����ת�����ָı���롣ת������ͨ�� CREATE CONVERSION ���塣��Ȼϵͳ����һЩԤ�����ת�����֡����� Table 9-7 ��ȡ���õ�ת������ convert('PostgreSQL' using iso_8859_1_to_utf_8)Unicode (UTF-8) �����'PostgreSQL'
lower(string)text���ִ�ת��ΪСдlower('TOM')tom
octet_length(string)integer�ִ��е��ֽ���octet_length('jose')4
position(substring in string)integer���������ִ���λ��position('om' in 'Thomas')3
overlay(string placing string from integer [for integer])text �滻���ִ� overlay('Txxxxas' placing 'hom' from 2 for 4)Thomas
position(substring in string)integerָ�������ִ���λ��position('om' in 'Thomas')3
substring(string [from integer] [for integer])text ��ȡ���ִ� substring('Thomas' from 2 for 3)hom
substring(string from pattern)text ��ȡƥ�� POSIX ������ʽ�����ִ� substring('Thomas' from '...$')mas
substring(string from pattern for escape)text ��ȡƥ��SQL������ʽ�����ִ� substring('Thomas' from '%#"o_a#"_' for '#')oma
trim([leading | trailing | both] [characters] from string) text ���ִ� string �� ��ͷ/��β/����/ ɾ��ֻ���� characters ��ȱʡ��һ���հף�������ִ��� trim(both 'x' from 'xTomxx')Tom
upper(string)text���ִ�ת��Ϊ��д��upper('tom')TOM

���ж�����ִ��������������ã�������Table 9-6�г��� ������Щ���ڲ�����ʵ��Table 9-5�г���SQL��׼�ִ�������

Table 9-6. �����ִ�����

�����������������������
ascii(text)integer������һ���ַ��� ASCII ��ascii('x')120
btrim(string text [, characters text])text �� string ��ͷ�ͽ�βɾ��ֻ������ characters �ȱʡ�ǿհף����ַ�����ִ��� btrim('xyxtrimyyx','xy')trim
chr(integer)text���� ASCII ����ַ�chr(65)A
convert(string text, [src_encoding name,] dest_encoding name) text ���ִ�ת��Ϊ dest_encoding . ԭ���ı������� src_encoding ������. ���ʡ���� src_encoding, �����Ϊ���ݿ����. convert('text_in_unicode', 'UNICODE', 'LATIN1')�� ISO 8859-1 �����ʾ��text_in_unicode
decode(string text, type text) bytea ��������encode����ģ������ string ����Ķ��������ݽ��롣 �������ͺ�encodeһ���� decode('MTIzAAE=', 'base64')123\000\001
encode(data bytea, type text) text �Ѷ��������ݱ���Ϊֻ���� ASCII ��ʽ�����ݡ� ֧�ֵ�������base64��hex��escape�� encode('123\\000\\001', 'base64')MTIzAAE=
initcap(text)text ��ÿ�����ʵĵ�һ����ĸתΪ��д�������ı���Сд�� ������һϵ����ĸ������ɵ��ַ����÷���ĸ���ַָ��� initcap('hi thomas')Hi Thomas
length(string text)integer string ���ַ�����Ŀ length('jose')4
lpad(string text, length integer [, fill text]) text ͨ������ַ� fill ��ȱʡʱΪ�հף��� �� string ���Ϊ���� length�� ��� string �Ѿ��� length ������ضϣ����ұߣ��� lpad('hi', 5, 'xy')xyxhi
ltrim(string text [, characters text]) text ���ִ� string �� ��ͷɾ��ֻ���� characters ��ȱʡ��һ���հף�������ִ��� ltrim('zzzytrim','xyz')trim
md5(string text)text ������� string �� MD5 ɢ�У���ʮ�����Ʒ��ؽ���� md5('abc')900150983cd24fb0d6963f7d28e17f72
pg_client_encoding()name ��ǰ�ͻ��˱������ơ� pg_client_encoding()SQL_ASCII
quote_ident(string text)text ���ظ����ִ���һ����������SQL����ִ��ﵱ����ʶ������ʹ�õ���ʽ�� ֻ���ڱ�Ҫ��ʱ��Ż�������ţ�Ҳ����˵������ִ������DZ�ʶ���ַ����߻�ת����Сд���ַ����� Ƕ������ű�ǡ����д��˫�ݡ� quote_ident('Foo bar')"Foo bar"
quote_literal(string text)text ���ظ����ִ���һ����������SQL����ִ��ﵱ���ı�ʹ�õ���ʽ�� Ƕ������źͷ�б�ܱ�ǡ����д��˫�ݡ� quote_literal('O\'Reilly')'O''Reilly'
repeat(string text, number integer)text�ظ� string number �Ρ�repeat('Pg', 4)PgPgPgPg
replace(string text, from text, to text)text���ִ�string����ֵ��������ִ� from �滻�����ִ� to�� replace('abcdefabcdef', 'cd', 'XX')abXXefabXXef
rpad(string text, length integer [, fill text]) text ͨ������ַ� fill ��ȱʡʱΪ�հף��� �� string ���Ϊ���� length�� ��� string �Ѿ��� length ������ضϡ� rpad('hi', 5, 'xy')hixyx
rtrim(string text [, character text]) text ���ִ� string �� ��βɾ��ֻ���� character ��ȱʡ�Ǹ��հף�������ִ��� rtrim('trimxxxx','x')trim
split_part(string text, delimiter text, field integer)text���� delimiter �ָ� string �������ɵĵ� field �����ִ���һΪ������ split_part('abc~@~def~@~ghi','~@~',2)def
strpos(string, substring)text ���������ִ���λ�á����� position(substring in stringһ����������Ҫע�����˳�� ���෴�ģ� strpos('high','ig')2
substr(string, from [, count])text ��ȡ���ִ������� substring(string from from for count)һ���� substr('alphabet', 3, 2)ph
to_ascii(text [, encoding])text �� text ����������ת��Ϊ ASCII�� [a] to_ascii('Karel')Karel
to_hex(number integer ���� bigint)text�� number ת�������Ӧ��ʮ�����Ʊ�����ʽ�� to_hex(9223372036854775807)7fffffffffffffff
translate(string text, from text, to text) text ���� string �а������κ�ƥ�� from �е��ַ����ַ�ת��Ϊ��Ӧ�� �� to �е��ַ��� translate('12345', '14', 'ax')a23x5
Notes:
a. to_ascii ����ֻ֧�ִ� LATIN1�� LATIN2��LATIN9 �� WIN1250 �������ת����

Table 9-7. ���õ�ת��

ת���� [a] Դ����Ŀ�ı���
ascii_to_micSQL_ASCIIMULE_INTERNAL
ascii_to_utf_8SQL_ASCIIUNICODE
big5_to_euc_twBIG5EUC_TW
big5_to_micBIG5MULE_INTERNAL
big5_to_utf_8BIG5UNICODE
euc_cn_to_micEUC_CNMULE_INTERNAL
euc_cn_to_utf_8EUC_CNUNICODE
euc_jp_to_micEUC_JPMULE_INTERNAL
euc_jp_to_sjisEUC_JPSJIS
euc_jp_to_utf_8EUC_JPUNICODE
euc_kr_to_micEUC_KRMULE_INTERNAL
euc_kr_to_utf_8EUC_KRUNICODE
euc_tw_to_big5EUC_TWBIG5
euc_tw_to_micEUC_TWMULE_INTERNAL
euc_tw_to_utf_8EUC_TWUNICODE
gb18030_to_utf_8GB18030UNICODE
gbk_to_utf_8GBKUNICODE
iso_8859_10_to_utf_8LATIN6UNICODE
iso_8859_13_to_utf_8LATIN7UNICODE
iso_8859_14_to_utf_8LATIN8UNICODE
iso_8859_15_to_utf_8LATIN9UNICODE
iso_8859_16_to_utf_8LATIN10UNICODE
iso_8859_1_to_micLATIN1MULE_INTERNAL
iso_8859_1_to_utf_8LATIN1UNICODE
iso_8859_2_to_micLATIN2MULE_INTERNAL
iso_8859_2_to_utf_8LATIN2UNICODE
iso_8859_2_to_windows_1250LATIN2WIN1250
iso_8859_3_to_micLATIN3MULE_INTERNAL
iso_8859_3_to_utf_8LATIN3UNICODE
iso_8859_4_to_micLATIN4MULE_INTERNAL
iso_8859_4_to_utf_8LATIN4UNICODE
iso_8859_5_to_koi8_rISO_8859_5KOI8
iso_8859_5_to_micISO_8859_5MULE_INTERNAL
iso_8859_5_to_utf_8ISO_8859_5UNICODE
iso_8859_5_to_windows_1251ISO_8859_5WIN
iso_8859_5_to_windows_866ISO_8859_5ALT
iso_8859_6_to_utf_8ISO_8859_6UNICODE
iso_8859_7_to_utf_8ISO_8859_7UNICODE
iso_8859_8_to_utf_8ISO_8859_8UNICODE
iso_8859_9_to_utf_8LATIN5UNICODE
johab_to_utf_8JOHABUNICODE
koi8_r_to_iso_8859_5KOI8ISO_8859_5
koi8_r_to_micKOI8MULE_INTERNAL
koi8_r_to_utf_8KOI8UNICODE
koi8_r_to_windows_1251KOI8WIN
koi8_r_to_windows_866KOI8ALT
mic_to_asciiMULE_INTERNALSQL_ASCII
mic_to_big5MULE_INTERNALBIG5
mic_to_euc_cnMULE_INTERNALEUC_CN
mic_to_euc_jpMULE_INTERNALEUC_JP
mic_to_euc_krMULE_INTERNALEUC_KR
mic_to_euc_twMULE_INTERNALEUC_TW
mic_to_iso_8859_1MULE_INTERNALLATIN1
mic_to_iso_8859_2MULE_INTERNALLATIN2
mic_to_iso_8859_3MULE_INTERNALLATIN3
mic_to_iso_8859_4MULE_INTERNALLATIN4
mic_to_iso_8859_5MULE_INTERNALISO_8859_5
mic_to_koi8_rMULE_INTERNALKOI8
mic_to_sjisMULE_INTERNALSJIS
mic_to_windows_1250MULE_INTERNALWIN1250
mic_to_windows_1251MULE_INTERNALWIN
mic_to_windows_866MULE_INTERNALALT
sjis_to_euc_jpSJISEUC_JP
sjis_to_micSJISMULE_INTERNAL
sjis_to_utf_8SJISUNICODE
tcvn_to_utf_8TCVNUNICODE
uhc_to_utf_8UHCUNICODE
utf_8_to_asciiUNICODESQL_ASCII
utf_8_to_big5UNICODEBIG5
utf_8_to_euc_cnUNICODEEUC_CN
utf_8_to_euc_jpUNICODEEUC_JP
utf_8_to_euc_krUNICODEEUC_KR
utf_8_to_euc_twUNICODEEUC_TW
utf_8_to_gb18030UNICODEGB18030
utf_8_to_gbkUNICODEGBK
utf_8_to_iso_8859_1UNICODELATIN1
utf_8_to_iso_8859_10UNICODELATIN6
utf_8_to_iso_8859_13UNICODELATIN7
utf_8_to_iso_8859_14UNICODELATIN8
utf_8_to_iso_8859_15UNICODELATIN9
utf_8_to_iso_8859_16UNICODELATIN10
utf_8_to_iso_8859_2UNICODELATIN2
utf_8_to_iso_8859_3UNICODELATIN3
utf_8_to_iso_8859_4UNICODELATIN4
utf_8_to_iso_8859_5UNICODEISO_8859_5
utf_8_to_iso_8859_6UNICODEISO_8859_6
utf_8_to_iso_8859_7UNICODEISO_8859_7
utf_8_to_iso_8859_8UNICODEISO_8859_8
utf_8_to_iso_8859_9UNICODELATIN5
utf_8_to_johabUNICODEJOHAB
utf_8_to_koi8_rUNICODEKOI8
utf_8_to_sjisUNICODESJIS
utf_8_to_tcvnUNICODETCVN
utf_8_to_uhcUNICODEUHC
utf_8_to_windows_1250UNICODEWIN1250
utf_8_to_windows_1251UNICODEWIN
utf_8_to_windows_1256UNICODEWIN1256
utf_8_to_windows_866UNICODEALT
utf_8_to_windows_874UNICODEWIN874
windows_1250_to_iso_8859_2WIN1250LATIN2
windows_1250_to_micWIN1250MULE_INTERNAL
windows_1250_to_utf_8WIN1250UNICODE
windows_1251_to_iso_8859_5WINISO_8859_5
windows_1251_to_koi8_rWINKOI8
windows_1251_to_micWINMULE_INTERNAL
windows_1251_to_utf_8WINUNICODE
windows_1251_to_windows_866WINALT
windows_1256_to_utf_8WIN1256UNICODE
windows_866_to_iso_8859_5ALTISO_8859_5
windows_866_to_koi8_rALTKOI8
windows_866_to_micALTMULE_INTERNAL
windows_866_to_utf_8ALTUNICODE
windows_866_to_windows_1251ALTWIN
windows_874_to_utf_8WIN874UNICODE
Notes:
a. ת������ѭһ����׼������ģʽ����Դ�����е����з���ĸ�����ַ� ���»����滻��������� _to_��Ȼ������ٸ��� ����ͬ�������Ŀ���������֡������Щ���ֿ��ܺͿͻ��ı������� ��ͬ��