• 技术文章 >数据库 >PostgreSQL

    postgresql 判断是否含某个字符

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-12-26 10:17:40原创3052

    判断字符串包含的几种方法:

    1、position(substring in string):

    1

    2

    3

    4

    5

    6

    7

    8

    9

    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)

    可以看出,如果包含目标字符串,会返回目标字符串笫一次出现的位置,可以根据返回值是否大于0来判断是否包含目标字符串。

    2、strpos(string, substring): 该函数的作用是声明子串的位置。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    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)

    作用与position函数一致。

    3、使用正则表达式:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    postgres=# select 'abcd' ~ 'aa'

    ?column?  ---------- 

    f (1 row) 

    postgres=# select 'abcd' ~ 'ab'

    ?column?  ---------- 

    t (1 row) 

    postgres=# select 'abcdab' ~ 'ab'

    ?column?  ----------

    t (1 row)

    Python学习网,大量的免费PostgreSQL入门教程,欢迎在线学习!

    专题推荐:postgresql 判断 字符
    上一篇:postgresql如何判断表是否存在 下一篇:postgresql如何创建表

    相关文章推荐

    • postgresql如何判断字段是否为空• postgresql如何判断表是否存在

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网