SQL - Dates


Date Types


  • DATE - YYYY-MM-DD
  • DATETIME - YYYY-MM-DD HH:MM:SS
  • TIMESTAMP - YYY-MM-DD HH:MM:SS
  • YEAR - YYYY or YY


Just match it like a string! Also, don’t attempt to match a datetime or timestamp to just its date. It won’t work.

SELECT * 
  FROM dates
  WHERE order_date = '2019-06-10'


In and Between

If you’re not looking to match a specific date, but several or even a range of dates, In and Between will be useful. In precedes a list of dates within parenthesis, and between allows you to list two dates and will find everything between them. These two operators aren’t only for dates, they can be used on all of the other data types too!

SELECT order_year
  FROM orders
  WHERE order_year IN ('2018', '2008', '1998')


SELECT order_year
  FROM orders
  WHERE order_year BETWEEN '2010' and '2015' 


That’s all for now!

- Fisher



Comments