Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi Guys,

Anyone has an idea why this code is generating this error msg
Msg 245, Level 16, State 1, Line 5
Conversion failed when converting the varchar value 'Jan ' to data type int.

DECLARE @Period	DATETIME 
SET @Period = '2013/01/01'


SELECT     Division, DSCustName, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year1' AND 
                      [Quarter] = '1' THEN ISNULL(Amount, 0) ELSE 0 END AS Year1Qtr1, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year1' AND 
                      [Quarter] = '2' THEN ISNULL(Amount, 0) ELSE 0 END AS Year1Qtr2, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year1' AND 
                      [Quarter] = '3' THEN ISNULL(Amount, 0) ELSE 0 END AS Year1Qtr3, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year1' AND 
                      [Quarter] = '4' THEN ISNULL(Amount, 0) ELSE 0 END AS Year1Qtr4, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year1' THEN ISNULL(Amount, 0) 
                      ELSE 0 END AS TotalYear1, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year2' AND [Quarter] = '1' THEN ISNULL(Amount, 0) 
                      ELSE 0 END AS Year2Qtr1, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year2' AND [Quarter] = '2' THEN ISNULL(Amount, 0) 
                      ELSE 0 END AS Year2Qtr2, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year2' AND [Quarter] = '3' THEN ISNULL(Amount, 0) 
                      ELSE 0 END AS Year2Qtr3, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year2' AND [Quarter] = '4' THEN ISNULL(Amount, 0) 
                      ELSE 0 END AS Year2Qtr4, CASE WHEN dbo.YearNumber(@Period, [Year]) = 'Year2' THEN ISNULL(Amount, 0) ELSE 0 END AS TotalYear2, 
CASE WHEN ISNULL(Product, '') = '' THEN 'No Product Group Name' ELSE Product END as Name
FROM         TableName 
WHERE ([Year] = LEFT(@Period, 4) OR [Year] = CASE WHEN CAST(RIGHT(@Period, 2) as INT) <= 6 THEN LEFT(@Period, 4) - 1 ELSE LEFT(@Period, 4) + 1 END)
Posted
Updated 7-Feb-14 7:18am
v3
Comments
Richard C Bishop 7-Feb-14 12:43pm    
It's pretty clear is it not? You are trying to convert a varchar to an int. Find where that is occurring and fix it.
Smith2014 7-Feb-14 12:48pm    
Did you look at the code at all?
Richard C Bishop 7-Feb-14 12:58pm    
It is not necessary for me to look at the code. I know exactly what that means. All you need to do is debug this and make the necessary changes. It says line 5, so I would start there. Your [Quarter] is probably 'Jan' since that is the first comparison with an int.
Smith2014 7-Feb-14 13:06pm    
I will appreciate a second opinion on it. seems you dont even understand what You're saying
Richard C Bishop 7-Feb-14 13:11pm    
Really? What is unclear about the exception that is being thrown and the way to fix it? You can't compare a varchar to an int, that is what you are doing. So you need to find where that is taking place(Line 5 as it states) and fix it. How is that at all ambiguous?

1 solution

I believe the declaration of "@Period" as a DATETIME is skewing the data you think you are getting. If "@Period" is not going to be dynamic and will always be '2013/01/01' then I would make it a VARCHAR(15) data type. If it is a dynamic date, then try converting that date into the varchar format specified in your setting of "@Period" variable before casting it and getting the RIGHT of LEFT side of the date. Let me know if that needs clarification.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900