Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Dates table YTD

Dates table YTD

Scheduled Pinned Locked Moved Database
database
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ruchi Gupta
    wrote on last edited by
    #1

    Hi All, Currently in my stored procedure I need to build a table with all dates starting 1st Jan to the date passed (YTD). Cuurently the way I do is, I start from first date of year and loop till @Datepassed, ignoring Sat and Sun. SELECT @tdate = @YEAR_start WHILE @tdate <= @Datepassed BEGIN if DATEPART (dw, @tdate) not in (1,7) INSERT INTO #DATES (tdate) VALUES (@tdate) SELECT @tdate = dateadd (dd,1,@tdate) END Foreg:- if dec 12 2003 is passed, looping becomes inefficient. Does any body know a better way. Thanks Ruchi

    H 1 Reply Last reply
    0
    • R Ruchi Gupta

      Hi All, Currently in my stored procedure I need to build a table with all dates starting 1st Jan to the date passed (YTD). Cuurently the way I do is, I start from first date of year and loop till @Datepassed, ignoring Sat and Sun. SELECT @tdate = @YEAR_start WHILE @tdate <= @Datepassed BEGIN if DATEPART (dw, @tdate) not in (1,7) INSERT INTO #DATES (tdate) VALUES (@tdate) SELECT @tdate = dateadd (dd,1,@tdate) END Foreg:- if dec 12 2003 is passed, looping becomes inefficient. Does any body know a better way. Thanks Ruchi

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Why rebuild something that is static? If you don't specify many various years, build either a global temp table or add a new table with incremental dates, ignoring Saturday and Sunday. To get the dates, then, just use something like:

      select Date into #DATES
      from DateCache
      where Date between @dateStart and @dateEnd

      Another way would be to use an extended stored proc. Programming something like this in C/C++ would be very efficient. What's questionable is how much more quickly you could build or fill a table.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups