SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    stored procedure date variable trouble

    Hey guys -

    Trying to return records from a database given a particular date range. Basically it returns the records fall in the past year at the point when the procedure is run...

    I hope that makes sense.
    Trouble is it won't run!

    Can someone take a look please?

    Thanks

    Code SQL:
    ALTER PROCEDURE [dbo].[delete_built_on]
     
     @START AS datetime,
     @END AS datetime
     
    AS
    BEGIN
     
    SET @START = GETDATE()
    SET @END = DateAdd(yy, -1, GetDate())
     
    SELECT decision_date, DATEDIFF(MONTH, decision_date, GETDATE()) AS 'How Old', application_id_int, claimant_id_int
    FROM application
    WHERE app_closed_date > @START AND app_closed_date < @END AND built_on_bit = 'TRUE'
    ORDER BY 'How Old' ASC
     
    END

    It is asking for values of start and end when I run this:

    Code SQL:
    EXEC delete_built_on

    Thanks again.

    DS

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,016
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    it looks like you don't intend to pass in the start/end date values

    try it like this --
    Code:
    ALTER PROCEDURE [dbo].[delete_built_on]
    AS
    BEGIN
    SELECT decision_date
         , DATEDIFF(month, decision_date, GETDATE()) AS 'How Old'
         , application_id_int
         , claimant_id_int
      FROM application
     WHERE app_closed_date >= DateAdd(yy, -1, GETDATE())
       AND app_closed_date  < GETDATE()
       AND built_on_bit = 'TRUE'
    ORDER 
        BY 'How Old' ASC
    END
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    it looks like you don't intend to pass in the start/end date values

    try it like this --
    Code:
    ALTER PROCEDURE [dbo].[delete_built_on]
    AS
    BEGIN
    SELECT decision_date
         , DATEDIFF(month, decision_date, GETDATE()) AS 'How Old'
         , application_id_int
         , claimant_id_int
      FROM application
     WHERE app_closed_date >= DateAdd(yy, -1, GETDATE())
       AND app_closed_date  < GETDATE()
       AND built_on_bit = 'TRUE'
    ORDER 
        BY 'How Old' ASC
    END

    Perfect.
    Thanks so much!
    I never thought to remove the variables and just use the date mechanism. Sometimes we just make things tough for ourselves!

    Cheers again.
    DS


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •