【问题标题】:SQL error while converting varchar to int by joining two tables通过连接两个表将 varchar 转换为 int 时出现 SQL 错误
【发布时间】:2017-07-06 10:06:07
【问题描述】:

我需要在我的数据库中查找缺失的数字。我正在比较两个数据库,在查询中创建的 tempdb,编号为 1 - 999 和 MYDAT。

MYDAT 看起来像:

+-------+
|  id   |
+-------+
| A-001 |
| A-002 |
| A-004 |
| A-... |
| A-952 |
| A-... |
+-------+

我正在运行这个查询:

declare @tempid int

set @tempid = 1

create table tempdb (tempid int)

while @tempid < 1000
begin
    insert into tempdb values(@tempid)
    set @tempid = @tempid + 1
end

select tempdb.tempid from tempdb
left join MYDAT on tempdb.tempid = CAST(SUBSTRING(MYDAT.ID, 3, 3) as INT)
where
MYDAT.ID IS NULL and
SUBSTRING(MYDAT.ID, 3, 3) <> '' and
SUBSTRING(MYDAT.ID, 3, 3) <> '000'and
SUBSTRING(MYDAT.ID, 3, 3)  NOT LIKE '%[^0-9]%'

drop table tempdb

在不删除 temdb 的情况下,select * from tempdb 看起来不错,我得到了我想要的。

从 MYDAT 选择和转换数据的部分效果很好,我只得到整数

select CAST(SUBSTRING(MYDAT.ID, 3, 3) as INT) fom MYDAT
where 
SUBSTRING(MYDAT.ID, 3, 3) <> '' and
SUBSTRING(MYDAT.ID, 3, 3) <> '000'and
SUBSTRING(MYDAT.ID, 3, 3)  NOT LIKE '%[^0-9]%'

我收到一个错误“将 varchar 转换为 int”,但我不知道为什么。当我将左连接更改为右连接时,我没有收到任何错误。

我还手动检查了两个数据库,没有字符串或字符,只有整数。

我也尝试了 CONVERT() 但结果相同。

有什么建议或想法有什么问题吗?

编辑:

1 - 我在 rextester 上尝试时发现一个错误。 I added MYDAT.ID IS NULL 查询所以我得到正确的结果。

2 - 示例 我需要这个:http://rextester.com/KFG73206

但 CAST 或 CONVERT 似乎不起作用http://rextester.com/WJIAH52304

【问题讨论】:

  • 标记您正在使用的 dbms。 (那里有一些特定于产品的结构......)
  • 对我来说,为什么您会收到此错误并不明显。您可以在Rextester 为我们创建一个演示并将链接粘贴到此处吗?
  • rextester.com/CSY70642 我唯一能想到的是 MYDAT 中的 ID 不是你想的那样.. 因为这看起来有效.. ID 如何在模式中定义 - 类型、长度等。
  • 所以 MYDAT.ID 的每个字符上的子字符串都不同...看看您是否在期望数字的地方有 alpha...(第 3、第 4、第 5 个字符)
  • 从 MYDAT 中选择 DISTINCT SUBSTRING(ID, 3,1);从 MYDAT 中选择 DISTINCT SUBSTRING(ID, 4,1);从 MYDAT 中选择 DISTINCT SUBSTRING(ID, 5,1);

标签: sql sql-server casting nvarchar sql-convert


【解决方案1】:

您确实说过“缺少数字”,所以 tempdb 中不在 MYDAT 中的东西就是您想要的吗?如果是这样,请参阅:http://rextester.com/HCB88714

【讨论】:

    【解决方案2】:

    无法明确说明原因,可能是数据问题。您可以尝试一些解决方法来避免强制转换,

     create table tempdb (tempid varchar(3))
    
    while @tempid < 1000
    begin
        insert into tempdb values(@tempid)
        set @tempid = @tempid + 1
    end
    
    select tempdb.tempid from tempdb
    left join MYDAT on tempdb.tempid = SUBSTRING(MYDAT.ID, 3, 3)
    where 
    SUBSTRING(MYDAT.ID, 3, 3) <> '' and
    SUBSTRING(MYDAT.ID, 3, 3) <> '000'and
    SUBSTRING(MYDAT.ID, 3, 3)  NOT LIKE '%[^0-9]%'
    

    【讨论】:

      【解决方案3】:

      问题在于where 子句不一定在on 子句之前执行。 SQL Server 可以重新排列操作。

      我猜你真的想与MYDAT.ID 的前三个字符进行比较。这稍微简化了一些事情,因为您可以使用LEFT(),如下面的代码所示。事实上,您的 where 条件看起来不正确,所以我修复了它们。

      最好的解决方案是try_convert():

      select tempdb.tempid
      from tempdb left join
           MYDAT
           on tempdb.tempid = try_convert(int, left(MYDAT.ID, 3) )
      where MYDAT.ID <> '' and
            left(MYDAT.ID, 3) <> '000' and
            left(MYDAT.ID, 3) NOT LIKE '%[^0-9]%';
      

      在 SQL Server 2012 之前的版本中,您可以改用 case:

      select tempdb.tempid
      from tempdb left join
           MYDAT
           on tempdb.tempid = (case when left(MYDAT.ID, 1, 3) not like '%[^0-9]%')
                                    then convert(int, left(MYDAT.ID, 3)
                               end)
      where MYDAT.ID <> '' and
            left(MYDAT.ID, 3) <> '000' and
            left(MYDAT.ID, 3) NOT LIKE '%[^0-9]%';
      

      case 确实保证了评估的顺序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-30
        • 1970-01-01
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 2013-11-27
        • 2013-07-21
        相关资源
        最近更新 更多