PostgreSQL判斷字符串包含的幾種方法:
方式一: position(substring in string):
position(substring in string)函數:參數一:目標字符串,參數二原字符串,如果包含目標字符串,會返回目標字符串笫一次出現的位置,可以根據返回值是否大于0來判斷是否包含目標字符串
select position('aa' in 'abcd');
position
----------
0
select position('ab' in 'abcd');
position
----------
1
select position('ab' in 'abcdab');
position
----------
1
方式二: strpos(string, substring)
strpos(string, substring)函數:參數一:原字符串,目標字符串,聲明子串的位置,作用與position函數一致。
select position('abcd','aa');
position
----------
0
select position('abcd','ab');
position
----------
1
select position('abcdab','ab');
position
----------
1
方式三:使用正則表達式
如果包含目標字符串返回t,不包含返回f
select 'abcd' ~ 'aa' as result;
result
------
f
select 'abcd' ~ 'ab' as result;
result
------
t
select 'abcdab' ~ 'ab' as result;
result
------
t
方式四:使用數組的@>操作符(不能準確判斷是否包含)
select regexp_split_to_array('abcd','') @> array['b','e'] as result;
result
------
f
select regexp_split_to_array('abcd','') @> array['a','b'] as result;
result
------
t
注意下面這些例子:
select regexp_split_to_array('abcd','') @> array['a','a'] as result;
result
----------
t
select regexp_split_to_array('abcd','') @> array['a','c'] as result;
result
----------
t
select regexp_split_to_array('abcd','') @> array['a','c','a','c'] as result;
result
----------
t
可以看出,數組的包含操作符判斷的時候不管順序、重復,只要包含了就返回true,在真正使用的時候注意。
到此這篇關于PostgreSQL判斷字符串是否包含目標字符串的文章就介紹到這了,更多相關PostgreSQL判斷字符串內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Postgresql 截取字符串的案例
- postgresql 查詢集合結果用逗號分隔返回字符串處理的操作
- postgresql 查詢字符串中是否包含某字符的操作
- PostgreSQL 使用raise函數打印字符串
- PostgreSQL 字符串拆分與合并案例
- postgreSQL 數字與字符串類型轉換操作