SQL Query WHERE等于int或varchar而不会中断?

时间:2015-09-09 15:50:28

标签: sql sql-server tsql case-when sql-convert

有一个实例,我的过滤器被动态插入到我的SQL查询中,有时无法告诉它们<tr style="background-color:yellow"> <td style="color:red font-weight:bold"> SERVER1 </td> <td style="color:red"> Sampler </td> <td style="color:red"> Offline </td> <td style="color:red"> No </td> </tr> 还是int。有没有办法允许使用varchar语句?

在我的表格(CASE WHEN for entity)中,列公司是字符串/ entvarchar列是entid。我似乎无法让这个int语句适用于int是过滤器或字符串是过滤器的情况。

这有效:

CASE IsNumeric

enter image description here

这是正在工作:

select 
    ent.company 
from 
    ent 
where 
    ent.company in
         (CASE 
             WHEN IsNumeric(CONVERT(VARCHAR(100), 'ABIS, Inc.')) = 0 
               THEN CONVERT(VARCHAR(100), 'ABIS, Inc.') 
               ELSE '99' 
          END,
          CASE 
             WHEN IsNumeric(CONVERT(VARCHAR(100), 'Schulte Building Systems')) = 0 
               THEN CONVERT(VARCHAR(100), 'Schulte Building Systems') 
               ELSE '1423' 
          END)

enter image description here

...但是对于int等于如果我将0更改为1则可以工作(如果我使用公司等于,则不返回任何内容)。知道为什么会失败吗?

1 个答案:

答案 0 :(得分:0)

解决了它。我从来没有问过实际的列 - 只是过滤器。

select ent.entid, ent.company from ent where 

ent.entid in
(
    CASE IsNumeric(entid) WHEN 1 then '99' else 'ABIS, Inc.' End,
    CASE IsNumeric(entid) WHEN 1 then '1423' else 'Schulte Building Systems' End    
)

select ent.entid, ent.company from ent where 

ent.company in
(
    CASE IsNumeric(company) WHEN 1 then '99' else 'ABIS, Inc.' End,
    CASE IsNumeric(company) WHEN 1 then '1423' else 'Schulte Building Systems' End  
)

都工作。

相关问题