Search code examples
sqlsql-likebetween

SQL query between two dates using regex


I'm doing a sql query to extract orders from date to date. I think I need to use LIKE because dates are strings and has days, but I have to consider only year and month, for example: order date -> 2019-01-01 regex -> LIKE '%2019-01-%' (all orders in done in january) and then I have to to the same thing for another date (let's say '%2019-03-%'). After that I use the two dates to extract time with BETWEEN. The problem is that I don't know how to put these two things together.


Solution

  • You can use the substring and then do comparison as follows:

    select * from orders
     where substring(date, 1, 7) between '2019-01' and '2019-03'
    

    Or you can convert your date column into date and then compare it with dates.