網站首頁 編程語言 正文
1. SPLIT_PART
SPLIT_PART() 函數通過指定分隔符分割字符串,并返回第N個子串。語法:
SPLIT_PART(string, delimiter, position)
- string : 待分割的字符串
- delimiter:指定分割字符串
- position:返回第幾個字串,從1開始,該參數必須是正數。如果參數值大于分割后字符串的數量,函數返回空串。
示例:
SELECT SPLIT_PART('A,B,C', ',', 2); -- 返回B
下面我們利用該函數分割日期,獲取年月日:
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
該函數用于分割字符串至數組元素,請看語法:
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函數返回表:
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的函數:
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}
于上面一樣,只是返回數組類型。
5. regexp_matches
該函數返回匹配模式的字符串數組。如果需要返回所有匹配的集合,則需要的三個參數‘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’ 參數,則僅返回第一項。
當然我們也可以使用regexp_replace函數進行替換:
select regexp_replace('yellow submarine', 'y[a-z]*w','blue');
-- 返回結果:blue submarine
總結
原文鏈接:https://blog.csdn.net/neweastsun/article/details/120243524
相關推薦
- 2022-05-20 flume的負載均衡load balancer
- 2022-04-07 Swift實現簡單計算器項目_Swift
- 2022-10-04 Qt計時器使用方法詳解_C 語言
- 2022-02-24 解決:this is incompatible with sql_mode=only_full_gr
- 2023-11-20 python獲取當前路徑所有文件
- 2021-12-02 C++內存分布及用法_C 語言
- 2022-08-23 C++深入淺出講解缺省參數_C 語言
- 2022-07-12 k8s 之 service ip
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支