Aruna Raghunam Aruna Raghunam - 6 months ago 15
SQL Question

Conversion error while converting varchar to smallint

Main Qualification : Varchar(20)

Qualification_Title: Varchar(100)

Query:

select distinct Main_Qualification,
Main_Qualification + ' '+ Qualification_Title As Main_Qual_Title
from dbo.IM_EN_Main_Qualifcation_with_title_vw
where College_Year = @College_Year
and Qualification_Title not Like '%CANX%'
order by Main_Qualification + ' '+ Qualification_Title


when I execute it I am getting error:

Conversion failed when converting the varchar value '20PA' to datatype smallint

dbo.IM_EN_Main_Qualifcation_with_title_vw:

SELECT dbo.IM_EN_Main_Qualification_vw.College_Year, dbo.IM_EN_Main_Qualification_vw.Learner_ID,
dbo.IM_EN_Main_Qualification_vw.Main_Qualification_Code,
dbo.IM_EN_Main_Qualification_vw.IS_AS_Level_Umbrella,
dbo.IM_EN_Main_Qualification_vw.IS_A2_Level_Umbrella,
dbo.IM_EN_Main_Qualification_vw.Main_Qualification,
dbo.IM_LU_Qualifications_vw.Qualification_Title,
dbo.IM_LU_Qualifications_vw.Qualification_Mgmnt_Code_1,
dbo.IM_LU_Qualifications_vw.Section
,dbo.IM_LU_Qualifications_vw.Qualification_Mgmnt_Code_2
,dbo.IM_EN_Main_Qualification_vw.Code
FROM dbo.IM_EN_Main_Qualification_vw INNER JOIN
dbo.IM_LU_Qualifications_vw ON
dbo.IM_LU_Qualifications_vw.Qualification_Code = dbo.IM_EN_Main_Qualification_vw.Main_Qualification_Code AND
dbo.IM_LU_Qualifications_vw.Qualification_Year = dbo.IM_EN_Main_Qualification_vw.College_Year


[dbo].[IM_EN_Main_Qualification_vw]( we are getting Main_Qualification from this view):

SELECT e.College_Year,
e.Learner_ID,
a.Code AS Main_Qualification_Code,
MAX(b.IS_AS_Level_Umbrella) AS IS_AS_Level_Umbrella,
MAX(b.IS_A2_Level_Umbrella) AS IS_A2_Level_Umbrella,
-- Not took MAX belkow because there were two people doing both AS and A2 Levels.
-- This will give them AS Level as Main Qualification.
MAX(CASE WHEN b.IS_AS_Level_Umbrella = 'Yes' THEN 'AS Level'
WHEN b.IS_A2_Level_Umbrella = 'Yes' THEN 'A2 Level'
ELSE a.Code END) AS Main_Qualification
,a.Code
FROM IM_EN_ENROLMENTS_VW e
INNER JOIN (
-- Join to the query that gets the qual with the maximum GLH
SELECT A.COLLEGE_YEAR,
A.LEARNER_ID,
MAX(A.CODE) AS CODE,
A.GLH
FROM (-- Get GKH of all Quals and join this to highest
-- GLH the student has beteewn all Quals
SELECT COLLEGE_YEAR,
LEARNER_ID,
CODE,
SUM(GLH) AS GLH
FROM DBO.IM_EN_ENROLMENTS_VW
WHERE (TYPE_OF_RECORD = 'Q')
AND (QUALIFICATION_MGMNT_CODE_1 NOT IN ('KS','TUT'))
AND (QUALIFICATION_MGMNT_CODE_3 NOT IN ('AD'))
AND (Code NOT LIKE ('%/N%'))
AND (Completion_Stat_Q19 <> '4')
GROUP BY COLLEGE_YEAR,LEARNER_ID,CODE) AS A
INNER JOIN (
-- Get the MAX GLH of all Quals
SELECT COLLEGE_YEAR,
LEARNER_ID,
MAX(GLH) AS MAXGLH
FROM (
-- Get GKH of all Quals
SELECT COLLEGE_YEAR,
LEARNER_ID,
CODE,
SUM(GLH) AS GLH
FROM DBO.IM_EN_ENROLMENTS_VW
WHERE (TYPE_OF_RECORD = 'Q')
AND (QUALIFICATION_MGMNT_CODE_1 NOT IN ('KS','TUT'))
AND (QUALIFICATION_MGMNT_CODE_3 NOT IN ('AD'))
AND (Code NOT LIKE ('%/N%'))
AND (Completion_Stat_Q19 <> '4')
GROUP BY COLLEGE_YEAR,LEARNER_ID,CODE) AS V
GROUP BY COLLEGE_YEAR,LEARNER_ID) AS B
ON A.COLLEGE_YEAR = B.COLLEGE_YEAR
AND A.LEARNER_ID = B.LEARNER_ID
AND A.GLH = B.MAXGLH
AND A.COLLEGE_YEAR = B.COLLEGE_YEAR
group by A.COLLEGE_YEAR,
A.LEARNER_ID,
A.GLH) AS a ON e.College_year = a.College_year AND e.Learner_ID = a.Learner_ID
LEFT JOIN (
-- Get AS and A2 Level Learners
SELECT College_Year,
Learner_Id,
Code,
dbo.IS_AS_Umbrella(College_Year, Code) AS IS_AS_Level_Umbrella,
dbo.IS_A2_Umbrella(College_Year, Code) AS IS_A2_Level_Umbrella
FROM IM_EN_ENROLMENTS_VW
WHERE (dbo.IS_AS_Umbrella(College_Year, Code) = 'Yes' OR dbo.IS_A2_Umbrella(College_Year, Code) = 'Yes')
AND Type_Of_Record = 'Q') AS b ON e.College_Year = b.College_year
AND e.Learner_ID = b.Learner_ID
GROUP BY
e.College_Year,
e.Learner_ID,
a.Code


IM_LU_Qualifications_vw(getting Qualification_Title from here):

SELECT RTRIM(dbo.PRPHProvisionHeader.PRPH_Code) AS Qualification_Code,
CASE WHEN LEFT(PRPH_Code, 5) = 'S1500' OR PRPH_Code LIKE '15%' THEN 'Yes' ELSE 'No' END AS Is_AS_level,
RTRIM(dbo.PRPIProvisionInstance.PRPI_Title) AS Qualification_Title,
------------------------------
RTRIM(PRIL_Title) as Course_funding_Title,
PRPIProvisionInstance.PRPI_Status,
------------------------------
dbo.PRILILR.PRIL_Year AS Qualification_Year,
RTRIM(dbo.PRPHProvisionHeader.PRPH_ML1) AS Qualification_Mgmnt_Code_1,
dbo.IM_LU_Departments_vw.Department,
RTRIM(dbo.PRPHProvisionHeader.PRPH_ML2) AS Qualification_Mgmnt_Code_2,
CASE WHEN RTRIM(dbo.PRPIProvisionInstance.PRPI_Available_Enr) = 0 THEN 'N' ELSE 'Y' END AS Available,
dbo.IM_LU_Sections_vw.Section,
RTRIM(dbo.PRPHProvisionHeader.PRPH_ML3) AS Qualification_Mgmnt_Code_3,
RTRIM(dbo.PRPIProvisionInstance.PRPI_FT_PT) AS Qualification_FT_PT,
ISNULL(RTRIM(dbo.PRCOCoordinators.PRCO_Tutor), '') AS Coordinator_Code,

ISNULL(PRFSFeesTuition.PRFS_Fee_Amount, 0) AS Tuition_Fees,

/********* TUI24+FC ************ */
ISNULL(FullCost_24plus.PRFS_Fee_Amount, 0) AS FullCost_24plus_Fees,

---------------
ISNULL(PRFSFeesUniform.PRFS_Fee_Amount, 0) AS Uniform_Fees,
ISNULL(PRFSFeesKit.PRFS_Fee_Amount, 0) AS Kit,
--------------
ISNULL(PRFSFeesMaterials.PRFS_Fee_Amount, 0) AS Material_Fees,
------------------------Full Cost Material_Fees-----------------------------------------------------------
ISNULL(FullCostMaterialsFees.PRFS_Fee_Amount, 0) AS FullCost_Material_Fees,

/********* CRB ************ */
ISNULL(CRB_Fees.PRFS_Fee_Amount, 0) AS CRB,
----------------------------------------------------------------------------------------------------------
ISNULL(PRFSFeesExam.PRFS_Fee_Amount, 0) AS Exam_Fees,
ISNULL(PRFSFeesTrainingCredit.PRFS_Fee_Amount, 0) AS Training_Credit_TF,
ISNULL(PRFSFeesUserFee5.PRFS_Fee_Amount, 0) AS Police_Check_Fees,
ISNULL(PRFSFeesOverseas.PRFS_Fee_Amount, 0) AS Overseas_Fees,
RTRIM(ISNULL(dbo.PRTTTutors.PRTT_Tutor, '')) AS Tutor,
dbo.PERSstaff.PERS_Forename AS Course_Leader_Forename,
dbo.PERSstaff.PERS_Surname AS Course_Leader_Surname,
RTRIM(dbo.PERSstaff.PERS_Department_ML) AS Tutor_Department,
IM_LU_Departments_vw.Department AS Tutor_Department_Description,
dbo.PRPIProvisionInstance.PRPI_GLH_A32 AS Qualification_GLH,
-------------------- Columns swapped around ------------
dbo.PRILILR.PRIL_Hours_per_Week AS GLH_per_week,
dbo.PRILILR.PRIL_Annual_End_Date As Annual_End_Date,
dbo.PRILILR.PRIL_Annual_GLH AS Annual_GLH,
--dbo.PRPIProvisionInstance.PRPI_Hours_per_Week AS GLH_per_week,
--------------------------------------------------------------
dbo.PRPIProvisionInstance.PRPI_Length_Weeks AS Weeks_in_year,
dbo.PRPIProvisionInstance.PRPI_Start_Date_A27 AS Qualification_Start_Date,
dbo.PRPIProvisionInstance.PRPI_Exp_End_Date_A28 AS Qualification_End_Date,
dbo.PRILILR.PRIL_Annual_Start_Date as Annual_Start_Date,
-------------------- Columns swapped around ------------
--dbo.PRILILR.PRIL_Aim_A09 AS Qual_Aim,
dbo.PRPIProvisionInstance.PRPI_Aim AS Qual_Aim,
dbo.PRPIProvisionInstance.PRPI_Title AS Aim_Title,
--+++++++++++++++++++++++++++++++++++
------------------- Columns swapped around ------------
ISNULL(AV.FullLevel2Percent, 0) AS Full_Level2_Percentage,
ISNULL(AV.FullLevel3Percent, 0) AS Full_Level3_Percentage,
--ISNULL(dbo.LAAV_ALL_VALUES.LEVEL2_PERCENTAGE, 0) AS Full_Level2_Percentage,
--ISNULL(dbo.LAAV_ALL_VALUES.LEVEL3_PERCENTAGE, 0) AS Full_Level3_Percentage,

-------------------- Columns swapped around ------------
[dbo].[Core_LARS_LearningDelivery].NotionalNVQLevel AS NVQLevel,
[dbo].[Core_LARS_LearningDelivery].EntrySubLevel as Entry_SubLevel,
[dbo].[Core_LARS_LearningDelivery].AwardOrgCode AS Awarding_Body,
-- dbo.LAIM_AIMS.NOTIONAL_NVQ_LEVEL_CODE AS NVQLevel,
-- dbo.LAIM_AIMS.AWARDING_ORGANISATION AS Awarding_Body,

--£££££££££££££££
dbo.PRILILR.PRIL_Year_Of AS QUAL_Year_of_Qual,
RTRIM(PRFSFeesCostRecovery.PRFS_Nominal) AS Cost_Recovery_AC,
RTRIM(PRFSFeesTuition.PRFS_Nominal) AS Tuition_Fees_AC,
Cost_Centres.Cost_Centre,
/********* TUI24+FC ************ */
FC_Cost_Centres.FC_Cost_Centre,

RTRIM(PRFSFeesTrainingCredit.PRFS_Nominal) AS Training_Credit_AC,
RTRIM(PRFSFeesExam.PRFS_Nominal) AS Exam_Fees_AC,
RTRIM(PRFSFeesMaterials.PRFS_Nominal) AS Material_Fees_AC,
RTRIM(PRFSFeesRegn.PRFS_Nominal) AS Regn_Fee_AC,
dbo.PRILILR.PRIL_Funding_A10 AS FEFC_Fund_Q10,
dbo.GNCDgncodes.GNCD_Description AS Funding_Description,
dbo.PRPIProvisionInstance.PRPI_Max AS QUAL_Max_Size,
NULL AS National_Rate_1,
dbo.PRPIProvisionInstance.PRPI_Length_Years AS No_Of_Years,
dbo.PRPIProvisionInstance.PRPI_Instance AS Instance,
dbo.PRPHProvisionHeader.PRPH_Type,
dbo.PRPIProvisionInstance.PRPI_Code AS Qual_Instance_Code,
-- dbo.PRPIProvisionInstance.PRPI_MOA AS MOA_Code,
dbo.PRILILR.PRIL_Workplace_Learning AS WPL,
------------------- Columns swapped around ------------
-- dbo.ALL_ANNUAL_VALUES.SSA_TIER1_CODE AS Subject_sector_area,
CASE WHEN [dbo].[Core_LARS_LearningDelivery].SectorSubjectAreaTier1 > 0
THEN RIGHT('0' + CAST(CAST([dbo].[Core_LARS_LearningDelivery].SectorSubjectAreaTier1 AS INT) AS VARCHAR),2) END AS Subject_sector_area,
--£££££££££££££
dbo.PRILILR.PRIL_Title as Marketing_Title,

dbo.PRILILR.PRIL_Delivery_Postcode_A23,
------------------------------------------------
RTRIM(dbo.PRPHProvisionHeader.PRPH_Subject) as Sub_Section
------------------------------------------------
FROM dbo.PRILILR LEFT OUTER JOIN
dbo.PRPHProvisionHeader INNER JOIN
dbo.PRPIProvisionInstance ON
dbo.PRPHProvisionHeader.PRPH_Code = dbo.PRPIProvisionInstance.PRPI_Code LEFT OUTER JOIN
dbo.PRCOCoordinators ON dbo.PRPHProvisionHeader.PRPH_Code = dbo.PRCOCoordinators.PRCO_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = dbo.PRCOCoordinators.PRCO_Instance

LEFT OUTER JOIN
[dbo].[Core_LARS_LearningDelivery] ON dbo.PRPIProvisionInstance.PRPI_Aim COLLATE Latin1_General_CI_AS = [dbo].[Core_LARS_LearningDelivery].[LearnAimRef] LEFT OUTER JOIN

(SELECT [dbo].[Core_LARS_AnnualValue].*
FROM [dbo].[Core_LARS_AnnualValue] INNER JOIN
(SELECT [LearnAimRef], MAX([EffectiveFrom]) LatestDate
FROM [dbo].[Core_LARS_AnnualValue]
GROUP BY LearnAimRef) LatestAV ON LatestAV.LearnAimRef = [dbo].[Core_LARS_AnnualValue].[LearnAimRef] AND LatestAV.LatestDate = [dbo].[Core_LARS_AnnualValue].[EffectiveFrom]) AV ON dbo.PRPIProvisionInstance.PRPI_Aim COLLATE Latin1_General_CI_AS = AV.LearnAimRef LEFT OUTER JOIN

dbo.PRFSFees AS PRFSFeesTuition ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesTuition.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesTuition.PRFS_Instance AND PRFSFeesTuition.PRFS_Fee_Type = 'TUICF' --'TUI'
/********* TUI24+FC ************ */
LEFT OUTER JOIN
dbo.PRFSFees AS FullCost_24plus ON dbo.PRPHProvisionHeader.PRPH_Code = FullCost_24plus.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = FullCost_24plus.PRFS_Instance AND (FullCost_24plus.PRFS_Fee_Type = 'TUI24+FC')
-----------------------
LEFT OUTER JOIN
dbo.PRFSFees AS PRFSFeesUniform ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesUniform.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesUniform.PRFS_Instance AND PRFSFeesUniform.PRFS_Fee_Type = 'UNIFORM'

LEFT OUTER JOIN
dbo.PRFSFees AS PRFSFeesKit ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesKit.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesKit.PRFS_Instance AND PRFSFeesKit.PRFS_Fee_Type = 'Kit'
--------------------

LEFT OUTER JOIN
dbo.PRFSFees AS PRFSFeesCostRecovery ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesCostRecovery.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesCostRecovery.PRFS_Instance AND
PRFSFeesCostRecovery.PRFS_Fee_Type = 'COST REC'

LEFT OUTER JOIN
dbo.PRFSFees AS PRFSFeesMaterials ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesMaterials.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesMaterials.PRFS_Instance AND PRFSFeesMaterials.PRFS_Fee_Type = 'MAT' LEFT OUTER JOIN
-----------------------------Full Cost Materials Fees---------------------------------------------------------------------------------------------------
dbo.PRFSFees AS FullCostMaterialsFees ON dbo.PRPHProvisionHeader.PRPH_Code = FullCostMaterialsFees.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = FullCostMaterialsFees.PRFS_Instance AND FullCostMaterialsFees.PRFS_Fee_Type = 'MATCF' LEFT OUTER JOIN
-----------------------------CRB Fees---------------------------------------------------------------------------------------------------
dbo.PRFSFees AS CRB_Fees ON dbo.PRPHProvisionHeader.PRPH_Code = CRB_Fees.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = CRB_Fees.PRFS_Instance AND CRB_Fees.PRFS_Fee_Type = 'UCF5' LEFT OUTER JOIN
--------------------------------------------------------------------------------------------------------------------------------------------------------
dbo.PRFSFees AS PRFSFeesExam ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesExam.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesExam.PRFS_Instance AND PRFSFeesExam.PRFS_Fee_Type = 'EXM' LEFT OUTER JOIN
dbo.PRFSFees AS PRFSFeesTrainingCredit ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesTrainingCredit.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesTrainingCredit.PRFS_Instance AND
PRFSFeesTrainingCredit.PRFS_Fee_Type = 'TRAIN CRED' LEFT OUTER JOIN
dbo.PRFSFees AS PRFSFeesOverseas ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesOverseas.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesOverseas.PRFS_Instance AND
PRFSFeesOverseas.PRFS_Fee_Type = 'NONEU' LEFT OUTER JOIN
dbo.PRFSFees AS PRFSFeesRegn ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesRegn.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesRegn.PRFS_Instance AND PRFSFeesRegn.PRFS_Fee_Type = 'REG' LEFT OUTER JOIN
dbo.PRFSFees AS PRFSFeesUserFee5 ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesUserFee5.PRFS_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesUserFee5.PRFS_Instance AND PRFSFeesUserFee5.PRFS_Fee_Type = 'UCF5' LEFT OUTER JOIN
dbo.PRTTTutors ON dbo.PRPHProvisionHeader.PRPH_Code = dbo.PRTTTutors.PRTT_Code AND
dbo.PRPIProvisionInstance.PRPI_Instance = dbo.PRTTTutors.PRTT_Instance LEFT OUTER JOIN
dbo.PERSstaff ON dbo.PRTTTutors.PRTT_Tutor = dbo.PERSstaff.PERS_Staff_Code LEFT OUTER JOIN
dbo.IM_LU_Sections_vw ON dbo.PRPHProvisionHeader.PRPH_ML2 = dbo.IM_LU_Sections_vw.Section_Code LEFT OUTER JOIN
dbo.IM_LU_Departments_vw ON dbo.PRPHProvisionHeader.PRPH_ML1 = dbo.IM_LU_Departments_vw.Department_Code LEFT OUTER JOIN
dbo.IM_LU_Departments_vw AS IM_LU_Departments_vw1 ON dbo.PERSstaff.PERS_Department_ML = IM_LU_Departments_vw1.Department_Code ON
dbo.PRILILR.PRIL_Code = dbo.PRPHProvisionHeader.PRPH_Code AND
dbo.PRILILR.PRIL_Instance = dbo.PRPIProvisionInstance.PRPI_Instance LEFT OUTER JOIN
dbo.GNCDgncodes ON dbo.GNCDgncodes.GNCD_General_Code = dbo.PRILILR.PRIL_Funding_A10 AND
dbo.GNCDgncodes.GNCD_Code_Type = 'CH' AND dbo.GNCDgncodes.GNCD_Level <= 98 LEFT OUTER JOIN
(SELECT PRFS_Code AS Code, PRFS_Instance AS Instance, RTRIM(PRFS_Finance_Dept) AS Cost_Centre
FROM dbo.PRFSFees
WHERE (PRFS_Fee_Type = 'TUICF')--'TUI'
GROUP BY PRFS_Code, PRFS_Instance, PRFS_Finance_Dept) AS Cost_Centres ON
dbo.PRPHProvisionHeader.PRPH_Code = Cost_Centres.Code AND dbo.PRPIProvisionInstance.PRPI_Instance = Cost_Centres.Instance

/********* TUI24+FC ************ */
LEFT OUTER JOIN
(SELECT PRFS_Code AS Code, PRFS_Instance AS Instance, RTRIM(PRFS_Finance_Dept) AS FC_Cost_Centre
FROM dbo.PRFSFees
WHERE (PRFS_Fee_Type = ('TUI24+FC'))--'TUI'
GROUP BY PRFS_Code, PRFS_Instance, PRFS_Finance_Dept) AS FC_Cost_Centres ON

dbo.PRPHProvisionHeader.PRPH_Code = FC_Cost_Centres.Code
AND dbo.PRPIProvisionInstance.PRPI_Instance = FC_Cost_Centres.Instance
-------------------

WHERE (dbo.PRPHProvisionHeader.PRPH_Type = 'Q')


Any help is appreciated.

Thanks,
Ar

Answer

You should look into dbo.IM_EN_Main_Qualifcation_with_title_vw. Your problem is that you are doing something like

c1 + c2

where c1 and c2 are columns, c1 is of type smallint, c2 is textual and when the view was created, c2 contained only numeric values, but recently a textual data was added, which contains '20PA'. You need to change the view to convert c1 to textual data, using CAST or CONVERT.

EDIT

As Aruna Raghuna pointed out, it turned out to be an issue in the College_Year column, which in at least a case, instead of the expected numeric value contained text as well. While I have shown the op the way to find the answer, frankly, it was Aruna, who searched for the issue. Thought process being used was as follows:

  • check column types and find out which are of smallint types, so that we will know where to look for the problem
  • check textual columns and find out which of those contained the text found in the error message
  • possible final step is to fix either the query or the functionality along with the data, so this kind of issue will not be recurring in the future
Comments