ITPub博客

首页 > IT职业 > IT生活 > postgresql 判断是否含某个字符

postgresql 判断是否含某个字符

IT生活 作者:deepocean601 时间:2021-09-11 10:24:46 0 删除 编辑


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

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)

可以看出,如果包含目标字符串,会返回目标字符串笫一次出现的位置,可以根据返回值是否大于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)

作用与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)

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2557/viewspace-2835057/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    102
  • 访问量
    475