mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add String Contains Another String as a postgres til.
This commit is contained in:
@@ -56,6 +56,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
- [Configure The Timezone](postgres/configure-the-timezone.md)
|
- [Configure The Timezone](postgres/configure-the-timezone.md)
|
||||||
- [Count Records By Type](postgres/count-records-by-type.md)
|
- [Count Records By Type](postgres/count-records-by-type.md)
|
||||||
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
||||||
|
- [String Contains Another String](postgres/string-contains-another-string.md)
|
||||||
- [Timestamp Functions](postgres/timestamp-functions.md)
|
- [Timestamp Functions](postgres/timestamp-functions.md)
|
||||||
- [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md)
|
- [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md)
|
||||||
- [Turning Timing On](postgres/turning-timing-on.md)
|
- [Turning Timing On](postgres/turning-timing-on.md)
|
||||||
|
|||||||
26
postgres/string-contains-another-string.md
Normal file
26
postgres/string-contains-another-string.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# String Contains Another String
|
||||||
|
|
||||||
|
You can check if a string *contains* another string using the `position`
|
||||||
|
function.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select position('One' in 'One Two Three');
|
||||||
|
position
|
||||||
|
----------
|
||||||
|
1
|
||||||
|
```
|
||||||
|
|
||||||
|
It returns the 1-based index of the first character of the first match of
|
||||||
|
that substring.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> select position('Four' in 'One Two Three');
|
||||||
|
position
|
||||||
|
----------
|
||||||
|
0
|
||||||
|
```
|
||||||
|
|
||||||
|
If the substring doesn't appear within the string, then the result is 0.
|
||||||
|
|
||||||
|
Thus, you can determine if a string *contains* another string by checking if
|
||||||
|
the value resulting from `position` is greater than 0.
|
||||||
Reference in New Issue
Block a user