Y
Y
ygen2016-09-02 14:40:41
MySQL
ygen, 2016-09-02 14:40:41

How to create a query like %-_[1-9]-%?

There is a database, it is impossible to form a request.
The table has a field with the following content: `10-08-2016`
I would like to make a selection by quarters. In this case, a sample of 9 months.
I use the query:
SELECT * FROM users.data WHERE date_sp LIKE "%-_[1-9]-%"
following the logic, it should display a record with the date `10-08-2016`. But unfortunately it returns an empty result.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pcdesign, 2016-09-02
@ygen

SELECT * FROM users.data WHERE STR_TO_DATE(date_sp,' %d-%m-%Y') > DATE_SUB(now(), INTERVAL 9 MONTH)

If the field is varchar

I
Immortal_pony, 2016-09-02
@Immortal_pony

Just in case.
LIKE does not know how to range valid values, but REGEXP can. And although in this case it would be more correct to use STR_TO_DATE, but in some similar cases this is not possible.
A solution to the problem with REGEXP:

SELECT * FROM users.data WHERE date_sp REGEXP "^(\d{2})-0[1-9]-(\d{4})$"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question