Skip to content
Advertisement

How can I select rows from database where column Name contains exactly 5 digits

I have a database with some count of rows. They contains information about books. And I need to select books with Name which contain EXACTLY 5 numbers. I tried to select by

SELECT * FROM books WHERE Name LIKE “*#*#*#*#*#*”

But result by this query returning books with names which contain more than 5 digits

For example, I have some rows (Names of books):

  1. To Kill a Mockingbird 2
  2. 1984 2
  3. The Lord of the Rings (The Lord of the Rings, #13)
  4. The Chronicles of Narnia (Chronicles of Narnia, #1,2,3,4,5,6,7)

And query, what I need, must return 2 but not 4 item

Advertisement

Answer

So, that was very and very easy.

SELECT *
FROM books
WHERE Name LIKE "*#*#*#*#*#*" AND Name NOT LIKE "*#*#*#*#*#*#*"
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement