-1

I'm working on a homework assignment, and all has been well until I got to this point. My professor wants me to pull only dates in MARCH, APRIL, and MAY, without using the BETWEEN operator.

NOTE: I'm not getting any errors. I am using EDUPE, which runs MySQL, but has small variances where some things simply won't work.

Question was, is there a way to make the code I have function properly? Or am I going in the wrong direction?

/*Exercise Six*/
SELECT order_id as "Order ID", DATE_FORMAT(order_date, '%M-%d-%Y') as "Order Date"
FROM orders
WHERE order_date IN ('March%', 'April%', 'May%')
ORDER BY order_date ASC;
Sierra
  • 327
  • 4
  • 11
  • Are you using MySQL? – potashin Mar 23 '16 at 00:38
  • You're asking about MySQL workbench, right? I'm actually running it in an online course provided compiler. Very interesting stuff, but I do have MySQL workbench if there's something you think I do better in there. – Sierra Mar 23 '16 at 00:40
  • 1
    I mean RDBMS, because date functions can differ for each of them. – potashin Mar 23 '16 at 00:42
  • Yeah, okay, I see what you mean...at least I think I see what you mean. I do need the day to be there though...but you're right, I need to find a way to pick only one year. – Sierra Mar 23 '16 at 00:52
  • It has been figured out. I'll just add on to Potashin's solution below: WHERE DATE_FORMAT(order_date, '%M') IN ('March', 'April', 'May') AND DATE_FORMAT(order_date, '%Y') IN ('2014') – Sierra Mar 23 '16 at 00:58
  • Tag dbms used. You've got product specific SQL there... – jarlh Mar 23 '16 at 07:59
  • The between operator is equivalent to using two `<=` and `>=` conditions. – wvdz Mar 23 '16 at 08:33
  • jarlh Should I suppose that product specific SQL is a bad thing? It's kind of hard to explain, but the compiler I'm using is an online thing that is used in my school but it runs MySQL. Not even sure how...well, common it is. It's EDUPE ... wvdz would those conditions work with dates? Perhaps if I did it in numerical months? – Sierra Mar 24 '16 at 02:32

2 Answers2

1

You can try with date_format again:

WHERE DATE_FORMAT(order_date, '%M') IN ('March', 'April', 'May')

Or just monthname():

WHERE MONTHNAME(order_date) IN ('March', 'April', 'May')
potashin
  • 44,205
  • 11
  • 83
  • 107
  • You have taughten me valuable content. Thank you! WHERE DATE_FORMAT(order_date, '%M') IN ('March', 'April', 'May') AND DATE_FORMAT(order_date, '%Y') IN ('2014') – Sierra Mar 23 '16 at 00:57
0

I'm not sure if this is the most efficient way, but you can do this with "union":

select order_ID, order_date from orders
where order_date Like '%Mar%'
union
select order_ID, order_date from orders
where order_date Like '%Apr%'
union
select order_ID, order_date from orders
where order_date Like '%May%'

EDIT: I prefer Otashin's answer.

Davy C
  • 639
  • 5
  • 16
  • Well it's still nice to know I have options. :) I've never used Union, so that will be fun to play with. – Sierra Mar 23 '16 at 00:48