0

I've tried to google the answer, but found nothing, also neither MySQL BETWEEN page nor MySQL Pattern matching page have any information about it.

So, I'm curious, is it correct to use SQL patterns in BETWEEN operator, like SELECT * FROM table WHERE date BETWEEN '2019-01-01%' AND '2019-02-01%' in case of date pattern is Y-m-d H:i.

It is not recommended to use SQL patterns in =, > and < cases, but nothing about BETWEEN

1 Answer 1

1

First, there is no like support with between. It just doesn't make sense.

Second, you should not be using string functions on dates. MySQL has lots of really useful string fucntions.

And finally, you can just use regular comparisons:

where date >= '2019-01-01' and date < '2019-02-02'

Ironically, this will work both when the date column is stored (properly) as a date. And also what it is stored as a datetime. And as a string (given the format you specify).

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly, I've wondered about it because of this works with datetime fine. So, it's just drops pattern in BETWEEN statement?
@Scilef . . . They are just characters in a string. And you are forcing type conversions for the comparison. And it might be affected by internationalization settings on your system. And it might prevent index usage. And accessing partitions in a partitioned table. An all-around bad idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.