472,127 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

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?

select CAST(DATEPART(m, getdate())-1 AS VARCHAR(2)) /*last month*/
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 1580
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.