PostgreSQL 判斷字符串包含的幾種方法


判斷字符串包含的幾種方法:

1. position(substring in string):

postgres=# select position('aa' in 'abcd');  position ----------   0 (1 row)  postgres=# select position('ab' in 'abcd');  position ----------   1 (1 row)  postgres=# select position('ab' in 'abcdab');  position ----------   1 (1 row)
 
 
 
         
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

可以看出,如果包含目標字符串,會返回目標字符串笫一次出現的位置,可以根據返回值是否大於0來判斷是否包含目標字符串。

2. strpos(string, substring): 該函數的作用是聲明子串的位置。

postgres=# select strpos('abcd','aa');  strpos --------   0 (1 row)  postgres=# select strpos('abcd','ab');  strpos --------   1 (1 row)  postgres=# select strpos('abcdab','ab');  strpos --------   1 (1 row)
 
 
 
         
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

作用與position函數一致。

3. 使用正則表達式:

postgres=# select 'abcd' ~ 'aa';  ?column? ----------  f (1 row)  postgres=# select 'abcd' ~ 'ab';  ?column? ----------  t (1 row)  postgres=# select 'abcdab' ~ 'ab';  ?column? ----------t (1 row)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM