how can I retrieve the data '2010/05' by using the getdate variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cheryl culwell
    New Member
    • Dec 2010
    • 4

    how can I retrieve the data '2010/05' by using the getdate variable

    The data in the table well_summary.ye armonth is defined as varchar(7) - I need to retrieve the previous month, get the current year & use those variables in a select to retrieve the previous month's data. I have formed the following select, but get an error with the "+" trying to add the '/' for the date. can someone help me correct the select?
    thanks

    select CAST(DATEPART(m , getdate())-1 AS VARCHAR(2)) /*last month*/
    SELECT CAST(DATEPART(Y YYY, GETDATE()) AS VARCHAR(4))
    UNION
    SELECT Wells.APICode, Well_Summary.Ye arMonth
    FROM Wells
    CROSS JOIN Well_Summary
    WHERE (Well_Summary.Y earMonth = CAST(DATEPART(Y YYY, GETDATE()) as varchar(2) + '/' + CAST(DATEPART(m , getdate())-1 as varchar(4) )
  • stepterr
    New Member
    • Nov 2007
    • 157

    #2
    Adding a ")" will get rid of the error but I think you'll still have a problem because you are trying to cast to a varchar(2) and it should be varchar(4).

    Here's the code for your where clause:
    Code:
    CAST(DATEPART(YYYY, GETDATE()) as varchar(4)) + '/' + CAST(DATEPART(m, getdate())-1 as varchar(4))

    Comment

    Working...