SQL query to find out if substring starts with
I have many books in my MySQL database. And I'm looking for specific book titles. I know how to find every heading that starts with the word "given":
SELECT * FROM books WHERE header LIKE 'dan%';
But how do you find a word (substring) inside a heading (string) that starts with the phrase "dan"?
Thanks in advance.
+3
Damjan Pavlica
source
to share
2 answers
Here's a quick and dirty trick: a word starts with dan
can be either at the beginning header
or after a space, so:
SELECT * FROM books WHERE header LIKE 'dan%' OR header like '% dan%';
+8
Mureinik
source
to share
A slight oversimplification is the following logic, which puts a space at the beginning of the title:
SELECT *
FROM books
WHERE ' ' || header LIKE ' dan%';
The only problem is that string concatenation is database dependent and the question doesn't mention database. Thus, it can also be:
WHERE ' ' + header LIKE ' dan%';
or
WHERE CONCAT(' ', header) LIKE ' dan%';
+2
Gordon linoff
source
to share