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

拟墨画扇 提交于 2019-12-12 19:10:16

问题


I tried the following stored procedure

ALTER PROCEDURE spvec
AS
SELECT Row_number() OVER (ORDER BY (SELECT 1)) AS 'RNumber'
    ,vf.*
FROM LOGIN ld
    ,vehicle vf
WHERE (ld.fid = vf.fid)
    AND ld.id <> 'NA'
    AND vf.CustID <> 'None'
    AND vf.Simo <> 'None'
ORDER BY Row_number() OVER (ORDER BY (SELECT 1))

there is none and NA text in row. e.g. there is not empty but there is values with none ,NA here WHERE (ld.fid = vf.fid)

UPDATE

ld.fid is string and vf.fid is in int

so i try to convert

ALTER PROCEDURE spvec
AS
SELECT Row_number() OVER (ORDER BY (SELECT 1)) AS 'RNumber'
    ,vf.*
FROM LOGIN ld
    ,vehicle vf
WHERE  CAST((ld.FFID=vf.FFID) AS INT)
    AND ld.id <> 'NA'
    AND vf.CustID <> 'None'
    AND vf.Simo <> 'None'
ORDER BY Row_number() OVER (ORDER BY (SELECT 1))\

this show following error on update

Incorrect syntax near the keyword 'AS'.

Incorrect syntax near ')'.

it throws the following error without update

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


回答1:


If your tables actually consist of None and NA, then try this :

ALTER PROCEDURE spvec
AS
SELECT Row_number() OVER (ORDER BY (SELECT 1)) AS 'RNumber'
    ,vf.*
FROM LOGIN ld
    ,vehicle vf
WHERE (ld.fid = cast(vf.fid as varchar(max)))
    AND cast(ld.id as varchar(2)) <> 'NA'
    AND cast(vf.CustID as varchar(4)) <> 'None'
    AND cast(vf.Simo as varchar(4)) <> 'None'
ORDER BY Row_number() OVER (ORDER BY (SELECT 1))



回答2:


Which values do you expect in ld.id, vf.CustID and vf.Simo? SQL Server has no concept of the meaning of NA or None. It will just compare two values...

If None means NULL (no value exists) you might use

WHERE vf.CustID IS NOT NULL

If you have a value indicating your None (e.g. negative numbers) you might use something like

WHERE vf.CustID>=0

In any case you have to be aware of datatypes, whenever you want to compare values (sorting, joining, filters, constraints...)

UPDATE

According to this

ld.fid is string and vf.fid is in int

the problem (or better: One of your problems) is here:

WHERE (ld.fid = vf.fid)

Again: What values do you expect? A JOIN (old-fashioned or real) connects rows with identical values. Since your vf.fid is INT there will be plain numbers, nothing else.

Whatever value is in ld.fid it will only be identical, if it is a plain number.

You did not mention your version of SQL Server. Starting with 2012 there are very handsome TRY_ functions. You've got some answers already...

With an earlier version you might try it with:

WHERE (ISNUMERIC(ld.fid) AND CAST(ld.fid AS INT) = vf.fid)

Another approach was a cast to VARCHAR like

WHERE (ld.fid=CAST(vf.fid AS VARCHAR(100)))

but you must be aware, that simple format differences like 100.0 or 100 (see the blanks!) would break this approach. If possible, try to keep it typesafe...

And don't forget: The error message you posted points to None and not to the joining fields. So I'm quite sure, that you have more than one type conversion issue...




回答3:


First you have to format your script.

The error occured dur to assigning a string value to an integer field in your table. most probably "ld.Id"

FOR SQL version from 2012.....

 ALTER PROCEDURE spvec
 AS
 BEGIN

   SELECT Row_number()OVER(ORDER BY (SELECT 1))  AS 'RNumber',
            vf.* 
    FROM login ld,
           JOIN vehicle vf  
               ON  TRY_CONVERT(INT, ld.fid) =vf.fid 
     WHERE  ISNULL(ld.id,'') <> 'NA'
           and ISNULL(vf.CustID,'') <> 'None' 
            and vf.Simo <> 'None'  
    Order by Row_number() OVER(ORDER BY (SELECT 1))

END

For Older versions.....

 ALTER PROCEDURE spvec
 AS
 BEGIN

   SELECT Row_number()OVER(ORDER BY (SELECT 1))  AS 'RNumber',
            vf.* 
    FROM login ld,
           JOIN vehicle vf  
               ON  CONVERT(INT, ld.fid) =CONVERT(INT,vf.fid )
     WHERE  ISNULL(ld.id,'') <> 'NA'
           and ISNULL(vf.CustID,'') <> 'None' 
            and vf.Simo <> 'None'  
            and ISNUMERIC(ld.fid)=1 AND ISNUMERIC(vf.fid)=1
    Order by Row_number() OVER(ORDER BY (SELECT 1))

END


来源:https://stackoverflow.com/questions/39221561/conversion-failed-when-converting-the-varchar-value-none-to-data-type-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!