偶遇一個需求:想按某個指定符號分割以後,提取字符。spa
例如:tag = '休閒,娛樂,運動,玩耍',想提取"休閒"這個詞。code
select string_to_array('休閒,娛樂,運動,玩耍',','); ------------------------------ -- {休閒,娛樂,運動,玩耍} -- (1 row)
select tag[1] from ( select string_to_array('休閒,娛樂,運動,玩耍',',') as tag ) a --------------------- -- 休閒 -- (1 row)
如此實現,可是string_to_array須要子查詢,當與其餘自動group by一塊兒查詢的時候就顯得及其不方便,所以可以使用方法二。blog
select split_part('休閒,娛樂,運動,玩耍',',',1); ----------------- -- 休閒 -- (1 row)
select split_part('abc~@~def~@~ghi','~@~',2) ----------------- -- def -- (1 row)
END 2018-08-01 17:03:18string