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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 To get next 7 unique future dates in query

Author  Topic 

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-11-22 : 12:14:40
I have a query like this

SELECT CONVERT(varchar, f.FixtureDate, 106) AS stringDate, DATENAME(DW, f.FixtureDate) AS DayOfWeek, f.FixtureDate, f.Meeting, f.VenueCode, f.Evening, f.BankHoliday,
f.Type, f.MeetingNum, f.Cancelled, v.Content_ID, f.Festival
FROM tbHRI_Fixtures AS f INNER JOIN
tbHRI_Venue AS v ON f.VenueCode = v.RA_Venue_id
WHERE (DATEDIFF(Day, f.FixtureDate, GETDATE()) <= 0)
ORDER BY f.FixtureDate, f.Festival DESC, f.Meeting

And works fine it returns dates between now and end of year.

But I have a requirement for on my screens which has 7 tabs, so I want a query giving me the next 7 dates (i.e FixtureDate) column, the reason why I don't just want to do date + 7 is because some dates dont have any entries.

Thanks

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-22 : 12:35:26
SELECT TOP 7 FixtureDate
FROM...
WHERE FixtureDate > GetDate()
ORDER BY FixtureDate

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2010-11-23 : 05:02:10
How can I also make sure they are distinct i.e no duplicates
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2010-11-23 : 06:59:08
-- *** Test Data ***
-- You should provide this!
CREATE TABLE #t
(
FixtureDate smalldatetime NOT NULL
)
INSERT INTO #t
SELECT '20101127'
UNION ALL SELECT '20101127'
UNION ALL SELECT '20101128'
UNION ALL SELECT '20101128'
UNION ALL SELECT '20101128'
UNION ALL SELECT '20101129'
UNION ALL SELECT '20101130'
UNION ALL SELECT '20101130'
UNION ALL SELECT '20101201'
UNION ALL SELECT '20101202'
UNION ALL SELECT '20101203'
UNION ALL SELECT '20101204'
UNION ALL SELECT '20101205'
UNION ALL SELECT '20101206'
UNION ALL SELECT '20101207'
-- *** End Test Data ***

SELECT DISTINCT TOP 7 FixtureDate
FROM #t
WHERE FixtureDate > CURRENT_TIMESTAMP
ORDER BY FixtureDate
Go to Top of Page
   

- Advertisement -