PostgreSQL字串處理函式

2021-06-18 06:43:44 字數 3123 閱讀 7672

,史上最全it資源,個人收藏總結!

前期準備,新建資料表

create table mytable (

id serial not null,

"name" character varying(20),

geom geometry,

remark character varying(255) )

插入資料

insert into mytable(name,geom,remark) values('test1','point(12 23)','remark1');

insert into mytable(name,geom,remark) values('test2','point(14 23)','remark2');

insert into mytable(name,geom,remark) values('test3','point(17 23)','remark3');

insert into mytable(name,geom,remark) values('test4','point(92 29)','remark4');

insert into mytable(name,geom,remark) values('test5','point(82 83)','remark5');

1、  字串連線符 '||'

例子:select name || remark as newname from mytable where name like '%test%';

結果:newname

"test1remark1"

"test2remark2"

"test3remark3"

"test4remark4"

"test5remark5"

2、  字串長度:bit長度bit_length(string),字元長度char_length(string)或者是character_length(string),位元組長度octet(string),具體的長度跟資料庫字元編碼有關,注意中文字元的長度

例子:select name,bit_length(name),char_length(name),octet_length(name) from mytable where name like '%test%';

結果"test1";40;5;5

"test2";40;5;5

"test3";40;5;5

"test4";40;5;5

"test5";40;5;5

3、  字串替換overlay(string placing string from int [for int])

例子:select name,overlay(name placing 'dd' from 5 for 2) from mytable where name like '%test%';

結果"test1";"testdd"

"test2";"testdd"

"test3";"testdd"

"test4";"testdd"

"test5";"testdd"

4、  查詢字串位子position(substring in string)

例子:select name,position('test' in name) from mytable where name like '%test%';

結果:"test1";1

"test2";1

"test3";1

"test4";1

"test5";1

5、  取子串,substring(string [from int] [for int]),substring(string from pattern)

例子1:select name,substring(name from 1 for 2) from mytable where name like '%test%';

結果1:

"test1";"te"

"test2";"te"

"test3";"te"

"test4";"te"

"test5";"te"

例子2:select name,substring(name from '^...') from mytable where name like '%test%';

結果2:以任何三個字元開頭的,以任何兩個字元結尾的為:substring(name from '..$')

"test1";"tes"

"test2";"tes"

"test3";"tes"

"test4";"tes"

"test5";"tes"

6、  轉換為大些和小寫:upper(string)和lower(string)

例子:select name,upper(name),lower(name) from mytable  where name like '%test%';

結果:"test1";"test1";"test1"

"test2";"test2";"test2"

"test3";"test3";"test3"

"test4";"test4";"test4"

"test5";"test5";"test5"

7、  去除開頭或者結尾或者全部的包含的字串trim([leading|trailing|both] [characters] from string)

例子:select name,trim(name),trim(leading 'test' from name),trim('1' from name) from mytable   where name like '%test%';

結果:"test1";"test1";"1";"test"

"test2";"test2";"2";"test2"

"test3";"test3";"3";"test3"

"test4";"test4";"4";"test4"

"test5";"test5";"5";"test5"

字串處理 字串反轉

請原諒博主今天很閒,於是乎博主又開始更新微博了。這次要更新的問題是 編寫乙個函式,反轉乙個單詞的順序。例如 do or do not,there is no try.就要反轉成 try.no is there not,do or do 大家要認真看看這道題,這道題和大家想象的貌似有點不同。首先字串反...

字串處理

uncode與ansi字串轉換 我們使用windows函式multibytetowidechar將多位元組字串轉換成寬字元字串。函式如下 int multibytetowidechar uintcodepage dworddwflags lpcstrlpmultibytestr intcbmulti...

字串處理

byte array new byte 2 array system.text.encoding.default.getbytes 啊 int i1 short array 0 0 int i2 short array 1 0 unicode解碼方式下的漢字碼 array system.text.e...