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)