Skip to content

Commit c2f4762

Browse files
committed
Fix #38 - Created rule G-3195: Always use wildcards in a LIKE clause.
1 parent 9d618b5 commit c2f4762

File tree

1 file changed

+35
-0
lines changed
  • docs/4-language-usage/3-dml-and-sql/1-general

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# G-3195: Always use wildcards in a LIKE clause.
2+
3+
!!! tip "Minor"
4+
Maintainability
5+
6+
## Reason
7+
8+
Using `like` without at least one wildcard (`%` or `_`) is unclear to a maintainer whether a wildcard is forgotten or it is meant as equality test. A common antipattern is also to forget that an underscore is a wildcard, so using `like` instead of equal can return unwanted rows. If the `char` datatype is involved, there is also the danger of `like` not using blank padded comparison where equal will. Depending on use case, you should either remember at least one wildcard or use normal equality operator.
9+
10+
## Example (bad)
11+
12+
``` sql
13+
select e.employee_id
14+
,e.last_name
15+
from employees e
16+
where e.last_name like 'Smith';
17+
```
18+
19+
## Example (good)
20+
21+
``` sql
22+
select e.employee_id
23+
,e.last_name
24+
from employees e
25+
where e.last_name like 'Smith%';
26+
```
27+
28+
## Example (good)
29+
30+
``` sql
31+
select e.employee_id
32+
,e.last_name
33+
from employees e
34+
where e.last_name = 'Smith';
35+
```

0 commit comments

Comments
 (0)