SQL Where Clause Date Parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • benchpolo
    New Member
    • Sep 2007
    • 142

    SQL Where Clause Date Parameter

    I wrote a SQL script with a rxfilldate (mm/dd/yyyy format) parameter in WHERE clause. Is there way to automate the where clause to pick up the previous months data.

    For example: DATEPAID BETWEEN '09/01/2007' AND '09/30/2007'

    where rxfilldate,112) BETWEEN
    convert(varchar (2),datepart(mm ,dateadd(month,-1,getdate()))) + '-01-' +
    convert(varchar (4),datepart(yy yy,dateadd(mont h,-1,getdate())))
    AND convert(varchar (20),getdate(), 10)

    Thanks.
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by benchpolo
    I wrote a SQL script with a rxfilldate (mm/dd/yyyy format) parameter in WHERE clause. Is there way to automate the where clause to pick up the previous months data.

    For example: DATEPAID BETWEEN '09/01/2007' AND '09/30/2007'

    where rxfilldate,112) BETWEEN
    convert(varchar (2),datepart(mm ,dateadd(month,-1,getdate()))) + '-01-' +
    convert(varchar (4),datepart(yy yy,dateadd(mont h,-1,getdate())))
    AND convert(varchar (20),getdate(), 10)

    Thanks.
    Is your above query not working??
    Make use of sysdate and subtract it by one month and get the first day and the last day of that month to get the previous months value..

    Comment

    • benchpolo
      New Member
      • Sep 2007
      • 142

      #3
      somehow, i am not getting any data.. my guess is because the date format for my rxfilldate is yyyymmdd, and i'm not quite sure how to write the statement based on yyyymmdd format.

      Comment

      • benchpolo
        New Member
        • Sep 2007
        • 142

        #4
        Below is the actual WHERE CLAUSE im using in my script

        CONVERT(varchar (8),pd.rxfillda te,112) BETWEEN
        convert(varchar (4),datepart(yy yy,dateadd(mont h,-1,getdate())))+
        convert(varchar (2),datepart(mm ,dateadd(month,-1,getdate())))+ '-01-'
        AND convert(varchar (20),getdate(), 10)

        Unfortunately, I am not getting date from last month. Please advise. Thanks.

        Comment

        • benchpolo
          New Member
          • Sep 2007
          • 142

          #5
          Here's another WHERE CLAUSE statement where the rxfilldate1 format is mm/dd/yy

          WHERE convert(varchar (8),pd.rxfillda te1,112) BETWEEN convert(varchar (2),datepart(mm ,dateadd(month,-1,getdate()))) + '-01-' +
          convert(varchar (4),datepart(yy yy,dateadd(mont h,-1,getdate())))
          AND convert(varchar (20),getdate(), 10)

          Comment

          Working...