0

我有两个使用相同代码的 SQL 服务器数据库。

我收到以下错误:

 Error when executing tsp_parse_str_int at line 26. Error message: Conversion failed when converting the nvarchar value '1000 ' to data type int.

如果您注意到有一个前导空格,但是在我的代码中,我将在转换之前删除空格!

    SET @temp = (select replace(@temp, ' ', ''))
    SET @projid = cast(@temp as int)

谢谢,布鲁斯

4

3 回答 3

1

试试这个

SET @temp = (select RTRIM(@temp))
SET @projid = cast(@temp as int)

RTRIM修剪右侧空间并LTRIM修剪左侧空间。同时删除左右两边的空格LTRIM(RTRIM(@temp))

于 2012-10-02T22:20:01.630 回答
1

我在 SQL Server 2012 中尝试了下面的代码,它完全没有任何修剪就可以工作。你确定它只是一个普通的''而不是另一个隐藏字符吗?

declare @temp nvarchar(20)
declare @projid int
set @temp = '  12345    '
--SET @temp = (select replace(@temp, ' ', ''))
SET @projid = cast(@temp as int)
select @projid
于 2012-10-02T22:30:49.117 回答
0

我发现它实际上不是一个空格,无论是回车制表符还是换行符,我都摆脱了:

替换(替换(替换(MyField,CHAR(10),''),CHAR(13),''),CHAR(9),'')

于 2012-10-03T02:59:13.163 回答