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
  • 704,970
  • 168
  • 1,303
  • 1,425
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
  • 60,688
  • 15
  • 118
  • 136
-2
SELECT *
FROM MyTable
WHERE DateColumn < 'UIDate'
ORDER BY DateColumn DESC
LIMIT 1
CristiC
  • 21,380
  • 12
  • 55
  • 87