判斷字符串包含的幾種方法:
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)