在WHERE子句中使用字符串值作为布尔值

时间:2016-04-14 13:14:46

标签: sql sql-server

是否可以在where子句中将值用作类型字符串。像这样的东西:

 select * from 
 (
 select datepart(dw,b.Date) as Tag,b.[Customer No_],b.Date,b.[POS Holiday],b.Nonworking,
 case
  when datepart(dw,b.Date)=1 then (select '[Valid at Monday]=1')
  when datepart(dw,b.Date)=2 then (select '[Valid at Tuesday]=1')
  when datepart(dw,b.Date)=3 then (select '[Valid at Wednesday]=1')
  when datepart(dw,b.Date)=4 then (select '[Valid at Thursday]=1')
  when datepart(dw,b.Date)=5 then (select '[Valid at Friday]=1')
  when datepart(dw,b.Date)=6 then (select '[Valid at Saturday]=1')
  when datepart(dw,b.Date)=7 then (select '[Valid at Sunday]=1')
 end as  TestB 
 from [CustomerBPCal] b
 ) as a 
 left join [CustomerShopAndArrivalTime] as c on c.[Customer No_]=a.[Customer No_]  where a.TestB 

如果是,我如何更改代码以运行此类查询?

1 个答案:

答案 0 :(得分:0)

标准属于WHERE子句。看来你只想这样做:

WHERE (datepart(dw, b.Date) = 1 And [Valid at Monday] = 1)
   OR (datepart(dw, b.Date) = 2 And [Valid at Tuesday] = 1)
   OR ...

或者,如果[Valid at ...]列属于外部联接表CustomerShopAndArrivalTime,那么当然在ON子句中:

ON c.[Customer No_] = a.[Customer No_]  
AND
(   (datepart(dw, b.Date) = 1 And c.[Valid at Monday] = 1)
 OR (datepart(dw, b.Date) = 2 And c.[Valid at Tuesday] = 1)
 OR ...
)