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. Month to date in SP

Month to date in SP

Scheduled Pinned Locked Moved Database
sharepointdatabasetoolsquestion
3 Posts 3 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    I want to create a Stored procedure that looks up data based off the last full month and also one for year to date data. These SP will fire as part as a script so there can be no user input regarding the dates. Is there a way to do this? Chris

    E 1 Reply Last reply
    0
    • A Anonymous

      I want to create a Stored procedure that looks up data based off the last full month and also one for year to date data. These SP will fire as part as a script so there can be no user input regarding the dates. Is there a way to do this? Chris

      E Offline
      E Offline
      Edbert P
      wrote on last edited by
      #2

      Sure. Just put a condition like below for data from the last month WHERE MONTH(dtmTransactionDate) = MONTH(DATEADD(m, -1, GETDATE())) AND YEAR(dtmTransactionDate) = YEAR(DATEADD(m, -1, GETDATE())) If you need to get specific date (e.g. starting from 15 of Jan to 15 of Feb) just add DAY condition to it. For year to date, just subtract 1 year from the current date using the DATEADD function. Edbert P. Sydney, Australia.

      M 1 Reply Last reply
      0
      • E Edbert P

        Sure. Just put a condition like below for data from the last month WHERE MONTH(dtmTransactionDate) = MONTH(DATEADD(m, -1, GETDATE())) AND YEAR(dtmTransactionDate) = YEAR(DATEADD(m, -1, GETDATE())) If you need to get specific date (e.g. starting from 15 of Jan to 15 of Feb) just add DAY condition to it. For year to date, just subtract 1 year from the current date using the DATEADD function. Edbert P. Sydney, Australia.

        M Offline
        M Offline
        michanne
        wrote on last edited by
        #3

        If you are interested in the last full month and need the last day - you run into problems with February. Try: declare @month char(10),@lastmonth datetime set @month = Convert(Char(6),getdate(),112)+'01' Print @month select @lastmonth = dateadd(m,-1,cast(@month as datetime)) print @lastmonth --First day of prior month select @lastmonth = dateadd(d,-1,cast(@month as datetime)) print @lastmonth --Last day of prior month Barbara

        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