在SQL中将varchar值转换为int错误时转换失败

时间:2015-09-01 05:23:54

标签: sql sql-server sql-server-2008

我只是想运行一个查询,但过了一段时间它就给了我转换失败错误。这是我试图运行的查询:

  

'将varchar值转换为int'

时转换失败
Declare @ID varchar(max)
set @ID = '33,  691, 688, 58,  42, 3'


select listing_id, rpt_id, name_en, phone
from tsgb_extract_110107 
where rpt_id in (@ID ) 

我将rpt_id作为varchar变量传递给查询。有什么想法吗?

6 个答案:

答案 0 :(得分:0)

将varchar与varchar进行比较可以解决您的问题,假设tsgb_extract_110107是一个表

select listing_id, rpt_id, name_en, phone
from tsgb_extract_110107 
where rpt_id in ('3300718',
'3305691',
'3234688',
'3358162',
'3423680',
'3406570',
'3368217',
'3363510',
) 

修改

您可以将@ID拆分为表,然后使用连接

Declare @ID varchar(max)
set @ID = '33,  691, 688, 58,  42, 3'


DECLARE @xml as xml
SET @xml = cast(('<X>'+replace(@ID, ',' ,'</X><X>')+'</X>') as xml)

;WITH criteria AS
(
    SELECT LTRIM(N.value('.', 'varchar(10)')) as value FROM @xml.nodes('X') as T(N)
)
select listing_id, rpt_id, name_en, phone
from tsgb_extract_110107 INNER JOIN criteria ON rpt_id = criteria.value

答案 1 :(得分:0)

试试这种方式

select listing_id, rpt_id, name_en, phone
from tsgb_extract_110107 
where ISNUMERIC(rpt_id) And Convert(numeric(18,0),rpt_id) in (3300718,
3305691,
3234688,
3358162,
3423680,
3406570,
3368217,
3363510,
) 

答案 2 :(得分:0)

首先,您需要一个字符串拆分器。这是一张取自Aaron Bertrand的article

CREATE FUNCTION [dbo].[SplitStrings_XML]
(
   @List       NVARCHAR(MAX),
   @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
   RETURN 
   (  
      SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
      FROM 
      ( 
        SELECT x = CONVERT(XML, '<i>' 
          + REPLACE(@List, @Delimiter, '</i><i>') 
          + '</i>').query('.')
      ) AS a CROSS APPLY x.nodes('i') AS y(i)
   );

然后,在WHERE子句中使用该函数:

select
    listing_id, rpt_id, name_en, phone
from tsgb_extract_110107 
where
    rpt_id in (
        select cast(rtrim(ltrim(value)) as int) from dbo.SplitStrings_XML(@ID)
    ) 

答案 3 :(得分:0)

使用以下查询

Declare @ID varchar(max)
set @ID = '33,  691, 688, 58,  42, 3'
declare @str nvarchar(max)

set @str = 'select listing_id, rpt_id, name_en, phone
from tsgb_extract_110107 
where rpt_id in ('+@ID+' )' 
exec sp_executesql @str

答案 4 :(得分:0)

试试这个样本

--create table tsgb_extract_110107 (rpt_id int , listing_id int, name_en varchar(50) , phone varchar(10))           

   --insert into tsgb_extract_110107 values (1,1,'abc','123'),(33,33,'abc','33'),(691,691,'abc','691'),(58,58,'abc','58'),(3,3,'abc','3')

   Declare @ID nvarchar(max)
   set @ID = '33,  691, 688, 58,  42, 3'

   Declare @sql nvarchar(max)
   set @sql = 'select listing_id, rpt_id, name_en, phone
   from tsgb_extract_110107 
   where rpt_id in ( '+ @ID + ' ) '
   select @sql
   EXEC sp_executesql @sql

答案 5 :(得分:0)

这个功能给我带来了什么帮助:

CREATE FUNCTION [dbo].[f_SplitString] (@sep char(1), @s varchar(512))
RETURNS table
AS
    RETURN (
        WITH Pieces(pn, start, stop) AS (
          SELECT 1, 1, CHARINDEX(@sep, @s)
          UNION ALL
          SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
          FROM Pieces
          WHERE stop > 0
       )
       SELECT pn,
       SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
       FROM Pieces
    )

放入像这样的where子句:

where Ids in (SELECT s FROM [dbo].[f_SplitString](',', @Ids))