postgresql----字符串函數與操作符


函數 返回值類型 描述 示例 結果
string||string text 字符串連接 select 'Post'||'gresql'||' good!'; Postgresql good!
string||non-string或non-string||string text 非字符串與字符串連接 select 1||' one'; 1 one
bit_length(string) int 字符串的位數,注意一個漢字是3個字節 select bit_length('one'); 24
char_length(string) int 字符串中字符的個數 select char_length('中國'); 2
length(string) int 字符串中字符個數,同char_length(string) select length('中國'); 2
length(string,encoding name) int 以name的編碼格式字符串的字符個數 select length('中國','GBK'); 3
octet_length(string) int 字符串的字節數 select octet_length('中國'); 6
lower(string) text 將字符串轉換成小寫 select lower('HeLLO'); hello
upper(string) text 將字符串轉換成大寫 select lower('HellO'); HELLO
initcap(string) text 將字符串中每個單詞的首字母大寫 select initcap('hello world !'); Hello World !
overlay(string placing string from int [for int]) text 替換字符串,其中第一個int是開始位置,第二個int是長度,如果沒有第二個int則長度默認為第二個字符串的長度 select overlay('Txxxxas' placing 'hom' from 2 for 4); Thomas
replace(string,string,string) text 替換字符串,將第一個字符串中的第二個字符串替換成第三個字符串 select replace('Txxxxas','xxxx','hom'); Thomas
translate(string textfrom textto text) text 把string中from字符替換成對應to中的字符,如('text','tx','nd')中t->n,x->d,替換后結果為nedn;如果from比to長,則刪除from多出的字符,如('Txxxxas','Txas','hom')結果為hooom;如果from中有重復字符,對應順序也是一一對應的,但是重復字符以第一個為准,如('txxxa','xxx','abcd'),結果為taaad

select translate('Txxxxas','xxxxa','hom');

select translate('Txxxxas','Txas','hom');

select translate('txxxa','xxxa','abcd');

Thhhhs

hoooom

taaad

position(substring in string) int 給定子字符串在字符串的位置 select position('lo' in 'hello'); 4
strpos(stringsubstring) int 功能同上,只是入參順序相反

select strpos('hello','lo');

4

substring(string from int  [for int]) text 截取字符串,從from位置截取長度for,如果for省略,則是從from至結尾

select substring('hello world' from 2 for 3);

select substring('hello world' from 2);

ell

ell world

substring(string,from int[, for int]) text 同上 select substring('hello world',2,3); ell
substring(string from pattern) text 截取匹配posix正則表達式的字符串 select substring('hello world' from '...$'); rld
substring(string from pattern for escape) text 截取匹配posix正則表達式的字符串,for為轉移字符 select substring('Thomas' from '%#"o_a#"_' for '#'); oma

trim([leading | trailing | both]

[characters] from string)

text 刪除字符串頭部(leading),尾部(trailing)或兩邊的空白活[characters]字符 select trim(both 'x' from 'xxjdhdxxxx'); jdhd

trim([leading | trailing | both]

[from] string[, characters] )

text 同上 select trim(both from '  jdhd   ',' '); jdhd
btrim(string text [characters text]) text 刪除字符串兩邊指定的字符 select btrim('xxhhhxxx','x'); hhh
rtrim(string text [characterstext]) text 刪除字符串尾部指定的字符 select rtrim('xxhhhxxx','x'); xxhhh
ltrim(string text [characterstext]) text 刪除字符串開頭指定的字符 select ltrim('xxhhhxxx','x'); hhhxxx
ascii(string) int 字符串第一個字符的ASCII值

select ascii('xa');

select ascii('x');

120

120

chr(int) text 將數字轉換成字符 select chr(65); A
concat(str "any" [, str "any" [, ...] ]) text 連接所有參數,個數不限,類型不限 select concat('x','man',3); xman3
concat_ws(sep textstr "any" [,str "any" [, ...] ]) text 功能同上,只是第一個參數是連接分隔符 select concat_ws(',','x','man',3); x,man,3
convert(string bytea,src_encoding namedest_encodingname) text 將字符串從指定編碼轉換至目的編碼格式 select convert('Hello','UTF8','GBK'); \x48656c6c6f
format(formatstr text [,formatarg "any" [, ...] ]) text 格式化字符串,類似C語言的sprintf,其中n$表示第n個參數 select format('Hello %s, %1$s', 'World'); Hello World, World
left(str textn int) text 返回字符串前n個字符,n為負數時返回除最后|n|個字符以外的所有字符 select left('hello',-2); hel
right(str textn int) text 返回字符串后n個字符,n為負數時返回除最前|n|個字符意外的所有字符 select right('hello',2); he
lpad(string textlength int [,fill text]) text 在字符串開頭填充text至長度為length,缺省為空白,如果string的長度已經大於length,則會截斷后面多余length的字符 select lpad('123',5,'0'); 00123
rpad(string textlength int [,fill text]) text 在字符串尾部填充text至長度為length,缺省為空白,如果string的長度已經大於length,則會截斷后面多余length的字符 select rpad('he',1,'o'); h
md5(string) text 計算string的md5散列值,並以十六進制返回 select md5('hello'); 5d41402abc4b2a76b9719d911017c592

parse_ident(qualified_identifiertext [, 

strictmode booleanDEFAULT true ] )

text[] 將qualified_identifier拆分解析到一個數組中,以句點為分隔符。 select parse_ident('SomeSchema.someTable'); {someschema,sometable}
pg_client_encoding() name 獲取當前客戶端編碼 select pg_client_encoding(); UTF8
quote_ident(string text) text 返回適合SQL語句標志符且使用適當引號的字符串,在字符串兩端加雙引號,如果字符串中出現雙引號,返回結果中將變成兩個,如果有2個連續的單引號,返回時只有1個 select quote_ident('Foo"''"bar'); "Foo""'""bar"
quote_literal(string text) text 功能同上,只是內嵌的單引號和雙引號被原樣保留 select quote_literal('Foo"''bar'); 'Foo"''bar'
quote_literal(value anyelement) text 將給定值轉成text select quote_literal(45); '45'
quote_nullable(string text) text 功能同quote_literal(string text),只是參數是NULL時返回NULL    
quote_nullable(value anyelement) text 功能同quote_literal(value anyelement),只是參數為NULL時返回NULL    
repeat(string textnumber int) text 將string重復number次 select repeat('Hi',2); HiHi
split_part(string text,delimiter textfield int) text 將字符串string以delimiter進行分割,並返回第field個子串 select split_part('1#2#3','#',2); 2
to_hex(number int or bigint) text 將數值轉換成十六進制 select to_hex(155); 9b
reverse(str) text 將字符串逆序輸出 select reverse('hello'); olleh
regexp_split_to_array(stringtextpattern text [, flags text]) text[] 將字符串匹配posix正則表達式分割為字符串數組 select regexp_split_to_array('hello world', E'\\s+'); {hello,world}
regexp_split_to_table(stringtextpattern text [, flagstext])

setof

text

功能同上,只是以單列形式返回 select regexp_split_to_table('hello world', E'\\s+');

hello
world

regexp_matches(string text,pattern text [, flags text]) setof text[] 返回string中第一個匹配posix正則表達式的子串,如果flag=g,則返回所有 select regexp_matches('foobarbequebaz', '(b..)','g');

{bar}
{beq}
{baz}

regexp_replace(string text,pattern text

replacement text[, flags text])

text 將匹配posix正則表達式的第一個子串替換成指定字符串,如果flag=g,則替換所有 select regexp_replace('Thomas', '.[mN]a.', 'M'); ThM

format格式化

格式說明符由 % 字符引進,格式為

%[ position ] type
組件的字段有:
position (optional)
n$ 格式的字符串,這里的n是要打印的參數的索引。索引為1表示在formatstr之后的第一個參數。如果省略了position,默認使用序列中的下一個參數。
type (required)
格式轉換的類型用來產生格式說明符的輸出。支持下列的類型:
s 格式參數值為簡單的字符串。空值作為空字符串對待。
I 將參數值作為SQL標識符對待,如果需要,雙寫它。值為空是錯誤的。
L 引用參數值作為SQL文字。空值用字符串 NULL 顯示,沒有引用。
除了上述的格式說明符,特殊的序列 %% 可以用作輸出 % 字符。

 

示例:

test=# SELECT format('Hello %s', 'World'); format -------------
 Hello World (1 row) test=# SELECT format('Testing %s, %s, %s, %%', 'one', 'two', 'three'); format ----------------------------
 Testing one, two, three, % (1 row) test=# SELECT format('INSERT INTO %I VALUES(%L)', 'Foo bar', E'O\'Reilly'); format ------------------------------------------- INSERT INTO "Foo bar" VALUES('O''Reilly') (1 row)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM