PostgreSQL 字符串處理與日期處理操作
字符串長度、大小寫
SELECT CHAR_LENGTH('test') -- 字符串長度
SELECT LENGTH('test')
LENGTH(string,encoding name)
SELECT LENGTH('測試','UTF-8');
LOWER(string) 或者 UPPER(string) -- 大小寫
ASCII(string)
SELECT ASCII('abc') -- 結(jié)果是'a'的ascii碼
字符串格式化
FORMAT(formatstr text [,formatarg "any" [, ...] ]) -- 類似于printf
字符串拼接
SELECT 'number' || 123 --字符串連接
CONCAT(str "any" [, str "any" [, ...] ])
CONCAT_WS(sep text, str "any" [,str "any" [, ...] ])
SELECT * FROM CONCAT_WS('#','hello','world')
字符串剪切與截取
LPAD(string text, length int [,fill text])
RPAD(string text, length int [,fill text])
SELECT LPAD('12345', 10,'0') -- 結(jié)果 "0000012345"
TRIM([leading | trailing | both] [characters] from string)
SELECT TRIM(both ' ' from ' hello world') -- 結(jié)果是'hello world'
BTRIM(string text [, characters text])
RTRIM(string text [, characterstext])
LTRIM(string text [, characterstext])
SELECT BTRIM('yyhello worldyyyy','y') -- 結(jié)果是'hello world'
LEFT(str text, n int) -- 返回字符串前n個(gè)字符,n為負(fù)數(shù)時(shí)返回除最后|n|個(gè)字符以外的所有字符
RIGHT(str text, n int)
SUBSTRING(string from int [for int])
SELECT SUBSTRING('hello world' from 7 for 5) -- 結(jié)果是'world'
字符串加引號(hào)
QUOTE_IDENT(string text)
QUOTE_LITERAL(STRING TEXT)
QUOTE_LITERAL(value anyelement)
SELECT 'l''host"' -- 結(jié)果是'l'host"'
SELECT QUOTE_LITERAL('l''host"') -- 結(jié)果是'l''host"'
字符串分割
SPLIT_PART(string text,delimiter text, field int)
REGEXP_SPLIT_TO_ARRAY(stringtext, pattern text [, flags text])
REGEXP_SPLIT_TO_TABLE(stringtext, pattern text [, flagstext])
SELECT SPLIT_PART('hello#world','#',2) -- 結(jié)果是'world'
SELECT REGEXP_SPLIT_TO_ARRAY('hello#world','#') -- 結(jié)果是{hello,world}
SELECT REGEXP_SPLIT_TO_TABLE('hello#world','#') as split_res -- 結(jié)果是兩行,第一行hello,第二行world
字符串查找、反轉(zhuǎn)與替換
POSITION(substring in string) -- 查找
SELECT POSITION('h' in 'hello world') -- 結(jié)果是1,這里從1開始計(jì)數(shù)
REVERSE(str)
REPEAT(string text, number int)
REPLACE(string,string,string)
SELECT REPLACE('hello world',' ','#')
REGEXP_MATCHES(string text,pattern text [, flags text])
REGEXP_REPLACE(string text,pattern text,replacement text[, flags text])
SELECT REGEXP_MATCHES('hello world','.o.','g') -- 返回兩行,第一行是'lo ',第二行是'wor'
SELECT REGEXP_MATCHES('hello world','.o.') -- 返回第一個(gè)匹配,'lo '
時(shí)間處理
SELECT TO_CHAR(TO_TIMESTAMP(CREATE_TIME),'YYYY-MM-DD HH24:MI:SS') SELECT EXTRACT(YEAR FROM NOW());
補(bǔ)充:postgresql處理時(shí)間函數(shù) 截取hh:mm/yyyy-mm-dd
1.to_timestamp:
AND to_timestamp(a.upload_time,'yyyy-MM-dd')>='"+startTime+"' and to_timestamp(a.upload_time,'yyyy-MM-dd') <= '"+endTime+"'
2.substring:
substring('2019-04-08 14:18:09',index,k):
數(shù)值代表含義 index:代表從index開始截取數(shù)據(jù),k代表從index開始截取到第k個(gè)數(shù)據(jù)
處理對(duì)象:時(shí)間為字符串格式的數(shù)據(jù)
eg:
截取時(shí)間到 年-月-日:
SELECT substring(upload_time,1,10) from table WHERE upload_time='2019-04-08 14:18:09'
結(jié)果:2019-04-08
截取時(shí)間到 時(shí):分:
SELECT substring(upload_time,12,5) from table WHERE upload_time='2019-04-08 14:18:09'
結(jié)果:14:18
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Windows下Postgresql數(shù)據(jù)庫的下載與配置方法
這篇文章主要介紹了Windows下Postgresql數(shù)據(jù)庫的下載與配置方法 ,需要的朋友可以參考下2014-06-06
postgresql兼容MySQL on update current_timestamp
這篇文章主要介紹了postgresql兼容MySQL on update current_timestamp問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Postgresql數(shù)據(jù)庫SQL字段拼接方法
Postgresql里面內(nèi)置了很多的實(shí)用函數(shù),下面這篇文章主要給大家介紹了關(guān)于Postgresql數(shù)據(jù)庫SQL字段拼接方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
postgresql 將逗號(hào)分隔的字符串轉(zhuǎn)為多行的實(shí)例
這篇文章主要介紹了postgresql 將逗號(hào)分隔的字符串轉(zhuǎn)為多行的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02

