PostgreSQL判斷字符串包含的幾種方法:
方式一: position(substring in string):
position(substring in string)函數:參數一:目標字符串,參數二原字符串,如果包含目標字符串,會返回目標字符串笫一次出現的位置,可以根據返回值是否大于0來判斷是否包含目標字符串
1
2
3
4
5
6
7
8
9
10
11
12
|
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函數一致。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
select position( 'abcd' , 'aa' ); position ---------- 0 select position( 'abcd' , 'ab' ); position ---------- 1 select position( 'abcdab' , 'ab' ); position ---------- 1 |
方式三:使用正則表達式
如果包含目標字符串返回t,不包含返回f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
select 'abcd' ~ 'aa' as result; result ------ f select 'abcd' ~ 'ab' as result; result ------ t select 'abcdab' ~ 'ab' as result; result ------ t |
方式四:使用數組的@>操作符(不能準確判斷是否包含)
1
2
3
4
5
6
7
8
9
|
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 |
注意下面這些例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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判斷字符串內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/L00918/article/details/113932089