Bob Senrie Bob Senrie - 1 month ago 8
SQL Question

Conversion failed when converting the nvarchar value '3255+' to data type int.

/****** Script for SelectTopNRows command from SSMS ******/
SELECT *


FROM Zuege as a

INNER JOIN [ResultateProzessModellZug_PF3_PF3_SimFV_2015_mNT_mSKT_L91_mZuschlaege_SKT] as b

ON a.ZuglaufID = b.Zuglaufid

LEFT JOIN [SysPue].[dbo].[VISUM_I_Knoten_Netz2015] as c
on [KnotenNummer] = Nr

LEFT JOIN [SysPue].[dbo].[LINIE_I_Richtung_Fahrplan2015] as d
on d.ZugNr = LineRouteName

WHERE Zugvariante = 216
and ModellzugID in (1,2,3,4,5,6,7,8,9,10)
and Qualitaetszug = 1
and Inland = 1


It's givin be back this ERROR:

Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the nvarchar value '3255+' to data type int.


I am really new to SQL i don't have an idea what might be wrong. Thanks for your help.

Bob

Answer Source

Look at the datatypes of every column that is involved in a comparison. Comparisons are found in the ON clauses of your JOINS, and in the WHERE clause.

Any time you are comparing a string (varchar) to a number (integer or other numeric type), you need to CAST the number-type column as a string (varchar). Otherwise SQL Server will try to implicitly CAST the string as a number, and if it can't, it will raise the error you are seeing.

So, if for example you find that Zugvariante is a varchar, then you need to make 216 a string by putting it in single quotes like this:

WHERE Zugvariante = '216'

If d.ZugNr is a number and LineRouteName is a string, then you need to compare them both as strings:

CAST(d.ZugNr AS varchar(31)) = LineRouteName