469,125 Members | 1,825 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,125 developers. It's quick & easy.

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

The data in the table well_summary.yearmonth 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(YYYY, GETDATE()) AS VARCHAR(4))
UNION
SELECT Wells.APICode, Well_Summary.YearMonth
FROM Wells
CROSS JOIN Well_Summary
WHERE (Well_Summary.YearMonth = CAST(DATEPART(YYYY, GETDATE()) as varchar(2) + '/' + CAST(DATEPART(m, getdate())-1 as varchar(4) )
Dec 14 '10 #1
1 1522
stepterr
157 100+
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:
Expand|Select|Wrap|Line Numbers
  1. CAST(DATEPART(YYYY, GETDATE()) as varchar(4)) + '/' + CAST(DATEPART(m, getdate())-1 as varchar(4)) 
Dec 16 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Miki | last post: by
11 posts views Thread by Tom Szabo | last post: by
3 posts views Thread by karthigaj | last post: by
8 posts views Thread by jj | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.