PostgreSQL 字符串處理與日期處理操作
更新時間:2021年02月01日 11:00:01 作者:巴拉巴拉朵
這篇文章主要介紹了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') -- 結果是'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') -- 結果 "0000012345"
TRIM([leading | trailing | both] [characters] from string)
SELECT TRIM(both ' ' from ' hello world') -- 結果是'hello world'
BTRIM(string text [, characters text])
RTRIM(string text [, characterstext])
LTRIM(string text [, characterstext])
SELECT BTRIM('yyhello worldyyyy','y') -- 結果是'hello world'
LEFT(str text, n int) -- 返回字符串前n個字符,n為負數(shù)時返回除最后|n|個字符以外的所有字符
RIGHT(str text, n int)
SUBSTRING(string from int [for int])
SELECT SUBSTRING('hello world' from 7 for 5) -- 結果是'world'
字符串加引號
QUOTE_IDENT(string text)
QUOTE_LITERAL(STRING TEXT)
QUOTE_LITERAL(value anyelement)
SELECT 'l''host"' -- 結果是'l'host"'
SELECT QUOTE_LITERAL('l''host"') -- 結果是'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) -- 結果是'world'
SELECT REGEXP_SPLIT_TO_ARRAY('hello#world','#') -- 結果是{hello,world}
SELECT REGEXP_SPLIT_TO_TABLE('hello#world','#') as split_res -- 結果是兩行,第一行hello,第二行world
字符串查找、反轉與替換
POSITION(substring in string) -- 查找
SELECT POSITION('h' in 'hello world') -- 結果是1,這里從1開始計數(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.') -- 返回第一個匹配,'lo '
時間處理
SELECT TO_CHAR(TO_TIMESTAMP(CREATE_TIME),'YYYY-MM-DD HH24:MI:SS') SELECT EXTRACT(YEAR FROM NOW());
補充:postgresql處理時間函數(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個數(shù)據(jù)
處理對象:時間為字符串格式的數(shù)據(jù)
eg:
截取時間到 年-月-日:
SELECT substring(upload_time,1,10) from table WHERE upload_time='2019-04-08 14:18:09'
結果:2019-04-08
截取時間到 時:分:
SELECT substring(upload_time,12,5) from table WHERE upload_time='2019-04-08 14:18:09'
結果:14:18
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
Windows下Postgresql數(shù)據(jù)庫的下載與配置方法
這篇文章主要介紹了Windows下Postgresql數(shù)據(jù)庫的下載與配置方法 ,需要的朋友可以參考下2014-06-06
postgresql兼容MySQL on update current_timestamp
這篇文章主要介紹了postgresql兼容MySQL on update current_timestamp問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Postgresql數(shù)據(jù)庫SQL字段拼接方法
Postgresql里面內(nèi)置了很多的實用函數(shù),下面這篇文章主要給大家介紹了關于Postgresql數(shù)據(jù)庫SQL字段拼接方法的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-11-11

