Wednesday, July 2, 2014

T-SQL: Get a list of dates

Returns a list of dates from first of year to end of year
DECLARE @startofyear DATETIME, @endofyear DATETIME
SET @startofyear =   DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
SET @endofyear = DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1);

WITH sample AS (
  SELECT @startofyear  AS [Date]  
  UNION ALL
  SELECT DATEADD(dd, 1, [Date])
    FROM sample s
   WHERE DATEADD(dd, 1, [Date]) <=  @endofyear) 
SELECT * 
  FROM sample
  option (maxrecursion 370) –loop not more than 370, change to zero to infinite



4 comments:

  1. Packers And Movers Bangalore Local Household Shifting Service, Get Free Best Price Quotes Local Packers and Movers in Bangalore List , Compare Charges, Save Money And Time @ Packers And Movers in Bangalore

    ReplyDelete
  2. Packers and Movers Pune Provide High Quality ***Household Shifting, Home/Office Relocation, Insurance, Packing, Loading, ###Car Transportation Service Pune and High experiences, Top Rated, Safe and Reliable, Best and Secure Packers and Movers Pune Team List. Get ✔✔✔ Affordable Rate Charts and Compare Quotation and Save Money and Time .
    Packers And Movers Pune

    ReplyDelete
  3. Packers and Movers Hyderabad Give Certified and Verified Service Providers, Cheap and Best ###Office Relocation Charges, ***Home Shifting, ???Goods Insurance worth Rs. 10,000, Assurance for Local and Domestic House Shifting. Safe and Reliable Household Shifting Services in Hyderabad with Reasonable Packers and Movers Price Quotation @
    Packers And Movers Hyderabad

    ReplyDelete