Msg 245, Level 16, State 1, Line 10. Conversion failed when converting the varchar value 'PAULBUDGET ' to data type int.

  • Hi there!!

    I get this error message:

    Msg 245, Level 16, State 1, Line 10

    Conversion failed when converting the varchar value 'PAULBUDGET ' to data type int.

    I don't know why because I did a cast to varchar.

    Can someone please help. Thanks.

    Line 10 is the first field in the select statement.

    SELECT cast(a.WS_Job_Number as varchar),

    a.Year1,

    a.PeriodID,

    CASE

    WHEN a.PeriodID = 1 THEN 'July'

    WHEN a.PeriodID = 2 THEN 'August'

    WHEN a.PeriodID = 3 THEN 'September'

    WHEN a.PeriodID = 4 THEN 'October'

    WHEN a.PeriodID = 5 THEN 'November'

    WHEN a.PeriodID = 6 THEN 'December'

    WHEN a.PeriodID = 7 THEN 'January'

    WHEN a.PeriodID = 8 THEN 'February'

    WHEN a.PeriodID = 9 THEN 'March'

    WHEN a.PeriodID = 10 THEN 'April'

    WHEN a.PeriodID = 11 THEN 'May'

    WHEN a.PeriodID = 12 THEN 'June'

    END AS 'Month',

    b.Cost_Element_Desc,

    rtrim(a.Cost_Code_Number_1) + rtrim(a.Cost_Code_Number_2) +

    rtrim(a.Cost_Code_Number_3) + rtrim(a.Cost_Code_Number_4) as Cost_Code,

    c.Cost_Code_Description,

    a.Cost_Code_Actual_Cost,

    a.Actual_Units_TTD,

    a.Revsd_Forecast_Cost,

    a.Revsd_Forecast_Units

    FROM JC20002 a

    inner join JC40209 b on a.Cost_Element = b.Cost_Element

    left join JC40202 c on a.Cost_Code_Number_1 = c.Cost_Code_Number_1

    and a.Cost_Code_Number_2 = c.Cost_Code_Number_2

    and a.Cost_Code_Number_3 = c.Cost_Code_Number_3

    and a.Cost_Code_Number_4 = c.Cost_Code_Number_4

    and a.Cost_Element = c.Cost_Element

    where WS_Job_Number = 1001

    and Year1 = 2017

    and PeriodID = 4

    order by 1,2,3,6

  • Here's a shoot-from-the-hip response first: You can get this sort of error by specifying a numeric value for comparison with a character type column in the WHERE clause. If most of the values in that column of the table look like numbers, the query may work up to the point where it hits the row with a non-numeric value. Check the datatypes for the columns in your WHERE clause -- it looks as though WS-Job-Number is a char or varchar datatype already and that "PAULBUDGET" is in one of the rows in that column. The cast in the SELECT part doesn't affect the WHERE clause; that part can probably be fixed by putting the target value in quotes:

    where WS_Job_Number = '1001'

    If that's not it, then please post the DDL for the three tables involved to give someone a better shot at figuring out what the trouble really is.

  • Hi. Thanks.

    That worked perfectly!!!

  • Hi Can anyone please help me, I get the following error message:

    Msg 245, Level 16, State 1, Line 2

    Conversion failed when converting the varchar value 'TA1001F ' to data type int.

    But my one column is char and the other one is varchar.

    How can I get this error about int then?

  • Hi JohnDean, and welcome to SS Central.

    You should post this question along with a bit more detail (table structure and the failing query) as a new post in an appropriate forum. Click Forums over on the left side, and drill down to one that fits your version of SQL Server and kind of question, which is apparently "T-SQL".

    You'll have a much better chance of getting help that way.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply