比较两个表中的所有数据,同时忽略某些差异

时间:2014-11-19 09:17:54

标签: sql sql-server compare

我目前正在尝试比较两个SQL Server数据库表。我在网上找到了各种方法,有些方法似乎有用,有些方法则没有。

我使用的那个是:

select * from table1 except select * from table2

当然唯一的问题(据我所知)是,表中说'NULL'值和'0'之间存在差异。这是正确的,存在差异。

但是我的问题是,有没有办法进行比较差异检查,同时忽略某些条件,如NULL和0。

1 个答案:

答案 0 :(得分:1)

您可以使用

select isnull(column1, 0) as column1 from table1 
except 
select isnull(column1, 0) as column1 from table2

将值0和null视为相同。

此外,如果您希望将更多的值视为null = 0 ='' (空字符串)

您可以使用案例:

select case when (column1 is null or column1 = '') then 0 end as column1 from table1
except 
select case when (column1 is null or column1 = '') then 0 end as column1 from table2