0

I have a table named Test and I want to display the info only from the current date.

I tried to do something like this but doesn't work if I use =

SELECT * FROM Test WHERE graduated = GETDATE()

And here is the table

------------------------------------------------
|  id         |  name           | graduated           |
------------------------------------------------
| 1      | John          | 2016-06-20         |
------------------------------------------------
| 2      | Amy          | 2016-06-20         |
------------------------------------------------
| 3      | Lily          | 2016-06-21         |
------------------------------------------------
Marc B
  • 356,200
  • 43
  • 426
  • 500
Phantom_strike
  • 149
  • 2
  • 10
  • what's the datatype for the `graduated` field? date? datetime? – Marc B Jun 20 '16 at 22:03
  • Also see [this Q&A](http://stackoverflow.com/q/1843395/335858). – Sergey Kalinichenko Jun 20 '16 at 22:04
  • The data type from the graduated field it's date – Phantom_strike Jun 20 '16 at 22:04
  • try `WHERE DATEDFIFF(day, graduated, GETDATE()) = 0`. `GETDATE` also includes the time component which is throwing off your equality check. – D Stanley Jun 20 '16 at 22:04
  • `SELECT * FROM Test WHERE graduated = CAST(GETDATE() AS DATE)` It is because GETDATE has hours min and seconds, and your dates do not. The casting as only date removes the hours min and seconds. – Arleigh Reyna Jun 20 '16 at 22:06
  • `WHERE CAST(graduated AS DATE) = CAST(GETDATE() AS DATE)` – M.Ali Jun 20 '16 at 22:20
  • Both getdate() and the valued in your graduated column include time components. For example, getdate() might return `2016-06-21 08:58:18.139`. This value is not equal to `2016-06-21 00:00:00.000` in the graduated column in the same way that `3.14159` does not equal `3.00000`. – Joel Coehoorn Jun 21 '16 at 13:59

0 Answers0