3

I have a date that I am grabbing from my UI (mm/dd/year AND hh:mi) and I need to find the nearest date (one record) BEFORE the date in my UI. I was researching, and it seems like DATEDIFF would be the best way to go about this? Or is there a better way to go about this? I'm a little unsure about the syntax. Thank you!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Johanna
  • 151
  • 1
  • 4
  • 8

2 Answers2

6
SELECT MAX(DateField)
FROM Table
WHERE Datefield < DateFromUI

This will get you the "newest" date that is older than the one passed in the WHERE clause. It should also be compatible with any RDBMS.

JNK
  • 63,321
  • 15
  • 122
  • 138
-2
SELECT *
FROM MyTable
WHERE DateColumn < 'UIDate'
ORDER BY DateColumn DESC
LIMIT 1
CristiC
  • 22,068
  • 12
  • 57
  • 89