>> Do I need to covert calyearmonth somehow?
Yes.
The problem here is that you cannot simply subtract 1. That is a math operation, and you are using an nvarchar. What's worse is that you could end up with faulty data. For example, what would happen if calmonthyear = '01/2008' Subtracting 1 should result in '12/2007'. So, to get the correct result, you need to convert this to a proper date, subtract 1 month, and then convert it back to your month/year format.
Let's do this in steps:
First, convert to a date. Since we don't care what the day is, we can simply use the first day of the month. so...
[!]'1/' + [/!]calymonthyear = '1/10/2007'
This is a well known date format widely used in europe. So, we can convert this to a proper datetime data type like this...
[!]Convert(DateTime,[/!] '1/' + CalYearMonth[!], 103)[/!]
The 103 (3rd parameter) tells SQL Server to use dd/mm/yyyy as the format.
Now that this is a proper date, we can subtract 1 month from it (using the DateAdd function).
[!]DateAdd(Month, -1, [/!]Convert(DateTime, '1/' + CalYearMonth, 103)[!])[/!]
We are adding a -1 month to the date, which will give us the first day of the previous month.
Now, we need to convert this back to your format MM/YYYY. There is no built in format for this, so we will need to do it in steps. First, convert it to a string while applying a dateformat of dd/mm/yyyy.
[!]Convert(VarChar(10),[/!] DateAdd(Month, -1, Convert(DateTime, '1/' + CalYearMonth, 103))[!], 103)[/!]
Now, we need to remove the first 3 characters, or, actually, return the last 7, so....
[!]Right([/!]Convert(VarChar(10), DateAdd(Month, -1, Convert(DateTime, '1/' + CalYearMonth, 103)), 103)[!], 7)[/!]
Make sense?
-George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom