0

I'm trying to use sql CURDATE(), in format "YYYY-MM-DD", to select items from yesterday but time column is in "YYYY-MM-DD HH-MM-SS" format.

Is it possible to put CURDATE between % %, something like in this example (which obviously doesn't work); SELECT * FROM table WHERE created_at LIKE % CURDATE() - INTERVAL 1 day %

Or if there is no syntax variation which makes use of curdate & also that works, do please recommend another way if there is any in sql bag of tricks.

1 Answers1

1

Don't use string functions as strings! If you want yesterday, you can use:

where created_at >= current_date - interval 1 day and
      created_at < current_date

Plus, this can use an index.

Or, you can simplify this to:

where date(created_at) = current_date - interval 1 day 

And this version does not use an index on created_at.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786