473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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?
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 1620
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

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

Similar topics

0
by: Miki | last post by:
Hi. I have a IIS server with php3 installed. I have SQL server database and data stored in unicode format (nvarchar fields). In header of my php I have the meta: <meta HTTP-EQUIV="content-type"...
2
by: kai | last post by:
Hi, All On Northwind database in SQL Server 2000, I try to setup default value at table design windows for "OrderDate" using GETDATE(), but it does not work. How do I use setup GETDATE default...
11
by: Tom Szabo | last post by:
I like to know if there is a JS function to retrieve a URL into a variable cURL = somefunction("http://www.domain.com"); Is there something like this? TIA, Tom
1
by: Epson Barnett | last post by:
Hi, I'm new to C# and I'd like to be able to reference a field of an object using a variable instead of literal text. In the PHP scripting language, you can create a variable: $var = "name";...
1
by: funcSter | last post by:
I want to retrieve data from an Excel file like how I would with a database. I understand that I would have to use OLE DB. Somehow I think I cannot get the connection string right, as the bit of...
5
by: ggk517 | last post by:
We are trying to develop an Engineering application using PHP, Javascript with Informix as the back-end. Is it possible to retrieve data using Javascript but by accessing the Database. Say...
3
by: karthigaj | last post by:
Hi, Happy new Year... i need to retrieve the data from the web server which is developed using HTML and i need to process those data in c but i should not use any CGIs. Can anybody help me...
1
by: sonalpathak | last post by:
hi i m working on windows application using c#. i have to retrieve data from database in checkbox control but its giving the following error---cannot convert object to bool. the code i am typing...
1
by: Ryan | last post by:
Hi, I'm trying to do retrieve some data from a table where the content is in Greek, however, the query is not working. It's a very simple statement, but I'm missing something. Here is the...
8
by: jj | last post by:
Trying to understand the manual regarding variables to solve the following. I'm working with Drupal CMS and have a loop to retrieve values that are normally fetched by using this variable:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.