Skip to content

Commit

Permalink
Create sqlalchemy-sql-like.md
Browse files Browse the repository at this point in the history
  • Loading branch information
andykee authored Nov 8, 2024
1 parent 4f15690 commit 5ff9eb9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions sqlalchemy/sqlalchemy-sql-like.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SQLAlchemy query LIKE statement

SQLAlchemy provides a number of methods for generating a `LIKE` clause in a query:

#### like
```python
select(User).where(User.name.like(f'%{some_term}%'))
```

#### contains
Search term inside % wildcards
```python
select(User).where(User.name.contains('i'))
```

#### startswith
Search term followed by % wildcard
```python
select(User).where(User.name.startswith('a'))
```

#### endswith
Search term preceeded by % wildcard
```python
select(User).where(User.name.endswith('e'))
```

Note that each of these has a case-insensitivt counterpart:
* `like` -> `ilike`
* `contains` -> `icontains`
* `startswith` -> `istartswith`
* `endswith` -> `iendswith`

Ref: https://stackoverflow.com/a/77111773

0 comments on commit 5ff9eb9

Please sign in to comment.