June 13, 2012 at 2:20 pm
Okay, I missed the "simple" ... I think Jared has it.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 13, 2012 at 8:57 pm
Adam Bean (6/12/2012)
Eugene Elutin (6/12/2012)
You don't need the cursor! If you have the calendar table, just select records you need filtering by date.Or you can use some run-time tally table like that:
DECLARE @DateFrom DATETIME
SET @DateFrom = '20120601'
SELECT TOP (DATEDIFF(DAY,@DateFrom,GETDATE()) + 1) DATEADD(DAY,ROW_NUMBER()
OVER (ORDER BY [object_id]) -1, @DateFrom ) AS MyDate
, @@SERVERNAME AS ServerName
, DB_NAME() AS DatabaseName
FROM sys.columns
You can see the maximum number of records it can return is limited by number of records in sys.columns, if you need more you can create Cartesian product of this table...
If you need some help with use of your calendar table, please post its DDL.
You sir, are my hero ... this is perfect and accomplished exactly what I wanted to do!
Thank you!
Now, a question for you. Do you know how it actually works?
--Jeff Moden
Change is inevitable... Change for the better is not.
June 19, 2012 at 9:42 am
The thing I'm not understanding is what purpose the TOP serves ... obviously because I am now running into this error: A TOP N value may not be negative.
I'm still parsing the data (this is insane data set), but I don't even see how it's possible to get a negative date; however I'm assuming it's breaking here: ORDER BY [CalendarDate]) -1
June 19, 2012 at 9:47 am
Adam Bean (6/19/2012)
The thing I'm not understanding is what purpose the TOP serves ... obviously because I am now running into this error: A TOP N value may not be negative.I'm still parsing the data (this is insane data set), but I don't even see how it's possible to get a negative date; however I'm assuming it's breaking here: ORDER BY [CalendarDate]) -1
Can you post the code you are running as well as the input value when you get the TOP N value may not be negative error?
June 19, 2012 at 11:56 am
Disregard, I found it ... was passing a date range on data that did not exist ... thanks all!
Viewing 5 posts - 16 through 19 (of 19 total)
You must be logged in to reply to this topic. Login to reply