Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
boab88
Starting Member
2 Posts |
Posted - 2009-04-21 : 09:05:03
|
| Hey.I'm looking for some help with an SQL statement to return only the record with the closest date to the current date.I have a table called fixtures with a column date. The date column is a datetime field in sql server.Currently Im using this statement:SELECT *FROM Fixtureswhere (some requirements) and date <GETDATE()This returns all the dates in the past (and i can change it to the furture with just >)but it can return multiple rows. How do I specifically only get the single closest row? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-21 : 09:09:49
|
SELECT TOP 1 *FROM FixturesWHERE (some requirements) AND Date < GETDATE()ORDER BY Date DESC E 12°55'05.63"N 56°04'39.26" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-21 : 09:11:31
|
| Try thisSELECT *FROM Fixtureswhere (some requirements) and date =(select max(date) from Fixtures where date<=GETDATE())MadhivananFailing to plan is Planning to fail |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2009-04-21 : 09:29:02
|
| He wants the closest date rather than the last eaarlier.select *from Fixtureswhere date = (select top 1 date from Fixtures order by abs(convert(float,getdate() - date)))==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|