網(wǎng)站首頁 編程語言 正文
1. SPLIT_PART
SPLIT_PART() 函數(shù)通過指定分隔符分割字符串,并返回第N個子串。語法:
SPLIT_PART(string, delimiter, position)
- string : 待分割的字符串
- delimiter:指定分割字符串
- position:返回第幾個字串,從1開始,該參數(shù)必須是正數(shù)。如果參數(shù)值大于分割后字符串的數(shù)量,函數(shù)返回空串。
示例:
SELECT SPLIT_PART('A,B,C', ',', 2); -- 返回B
下面我們利用該函數(shù)分割日期,獲取年月日:
select split_part( current_date::text,'-',1) as year ,
split_part( current_date::text,'-',2) as month,
split_part( current_date::text,'-',3) as day
返回信息:
year | month | day |
---|---|---|
2021 | 09 | 11 |
2.STRING_TO_ARRAY
該函數(shù)用于分割字符串至數(shù)組元素,請看語法:
string_to_array(string, delimiter [, null string])
- string : 待分割的字符串
- delimiter:指定分割字符串
- null string : 設定空串的字符串
舉例:
SELECT string_to_array('xx~^~yy~^~zz', '~^~'); -- {xx,yy,zz}
SELECT string_to_array('xx~^~yy~^~zz', '~^~', 'yy'); -- {xx,,zz}
我們也可以利用unnest函數(shù)返回表:
SELECT t as name
FROM unnest(string_to_array('john,smith,jones', ',')) AS t;
name |
---|
john |
smith |
jones |
3. regexp_split_to_array
使用正則表達式分割字符串,請看語法:
regexp_split_to_array ( string text, pattern text [, flags text ] ) → text[]
請看示例:
postgres=# SELECT regexp_split_to_array('foo bar baz', '\s+');
regexp_split_to_array
-----------------------
{foo,bar,baz}
(1 row)
當然也有對應可以返回table的函數(shù):
SELECT t as item
FROM regexp_split_to_table('foo bar,baz', E'[\\s,]+') AS t;
返回結果:
item |
---|
foo |
bar |
baz |
4.regexp_split_to_array
select regexp_split_to_array('the,quick,brown;fox;jumps', '[,;]') AS subelements
-- 返回 {the,quick,brown,fox,jumps}
于上面一樣,只是返回數(shù)組類型。
5. regexp_matches
該函數(shù)返回匹配模式的字符串數(shù)組。如果需要返回所有匹配的集合,則需要的三個參數(shù)‘g’ (g 是 global 意思)。請看示例:
select regexp_matches('hello how are you', 'h[a-z]*', 'g')
as words_starting_with_h
返回結果:
words_starting_with_h |
---|
{hello} |
{how} |
如果忽略 ‘g’ 參數(shù),則僅返回第一項。
當然我們也可以使用regexp_replace函數(shù)進行替換:
select regexp_replace('yellow submarine', 'y[a-z]*w','blue');
-- 返回結果:blue submarine
總結
原文鏈接:https://blog.csdn.net/neweastsun/article/details/120243524
相關推薦
- 2022-08-11 C#實現(xiàn)快速查詢文件的方法_C#教程
- 2023-01-12 C語言技巧提升之回調(diào)函數(shù)的掌握_C 語言
- 2022-10-21 IDEA集成Docker實現(xiàn)一鍵部署的詳細過程_docker
- 2022-09-21 Android開發(fā)Activity的生命周期詳解_Android
- 2022-06-09 FreeRTOS動態(tài)內(nèi)存分配管理heap_2示例_操作系統(tǒng)
- 2022-03-14 has been blocked by CORS policy: Response to prefl
- 2022-10-03 React?正確使用useCallback?useMemo的方式_React
- 2022-03-12 Nginx熱部署的實現(xiàn)_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支