TSQL - 排除条件

时间:2011-09-22 13:43:04

标签: tsql

我正在尝试构建一个SQL脚本,它将生成一个用于正向转换的字段列表。我的意思是,当应用程序最初部署时,我们不得不将旧数据迁移到新数据库中,因为旧数据不符合新标准。我没有进行迁移,只是清理它。

设计

select * from tblclient
where LEN(clientmatter) <> 11
    and clientmatter not in (select * from tblclient
                where ISNUMERIC(clientmatter) <> 1)

我在语法上知道这不起作用,但设计应该提供我想要做的事情。

条件中的派生表将与Union All中的此辅助表一起使用。我试图从长度问题结果中排除非数字结果,这样我就可以得到两个条件的完整信息。

经过测试的设计:

select 'non-numeric', clientmatter
from tblclient
where ISNUMERIC(clientmatter) <> 1
UNION ALL
select 'length problem', clientmatter
from tblclient
where LEN(clientmatter) <> 11

试过这个,但我得到了重复。我记得做了一个脚本,我联合了两个查询,但只产生了一个结果,但记得我是怎么做的。

2 个答案:

答案 0 :(得分:1)

SELECT clientmatter,
       CASE
         WHEN LEN(clientmatter) <> 11 THEN 'length problem'
       END,
       CASE
         WHEN ISNUMERIC(clientmatter) <> 1 THEN 'non-numeric'
       END
FROM   tblclient
WHERE  ISNUMERIC(clientmatter) <> 1
        OR LEN(clientmatter) <> 11  

答案 1 :(得分:0)

试试这个

select 'non-numeric', clientmatter
from tblclient
where ISNUMERIC(clientmatter) <> 1 and LEN(clientmatter)=11
UNION ALL
select 'length problem', clientmatter
from tblclient
where LEN(clientmatter) <> 11 and IsNumeric(clientMatter)=1
UNION ALL
select 'both problems', clientmatter
from tblclient
where LEN(clientmatter) <> 11 and IsNumeric(clientMatter)=0