Results 1 to 4 of 4

Thread: case statement in where clause?

  1. #1
    Join Date
    Feb 2003
    Location
    Asheville NC
    Posts
    44

    case statement in where clause?

    Can I do the following? Keep getting an error stopping at the first < of the where clause.


    declare @mon as int, @yr as int, @myDate as varChar(20)
    set @yr=2006
    set @mon=1
    set @mydate='01/31/2006 23:59:59'

    select 0 as DTAP, 0 as DT, 0 as TD, 0 as HIB, 0 as IPV, 0 as MMR, 0 as HEPB, 0 as _VAR, count(v.procedureKey) as FLU, 0 as PPV23, 0 as PCV7, v.chartID, max(rs1.dateService) as dateService from dbo.tbl1 v,
    (select distinct dateService, chartID, procedureKey from fhc.dbo.tbl1 where (datePart(year,dateService)=@yr and datePart(month,dateService)=@mon) and (procedureKey='90657' or procedureKey='90658')) as rs1
    where (v.chartID=rs1.chartID) and (v.procedureKey=rs1.procedureKey) and
    (case when @mon=1 then dateDiff(month,dateService,@myDate)< 216
    when @mon=2 then dateDiff(month,dateService,@myDate)<244
    when @mon=3 then dateDiff(month,dateService,@myDate)<275
    when @mon=4 then dateDiff(month,dateService,@myDate)<305
    when @mon=5 then dateDiff(month,dateService,@myDate)<336
    when @mon=6 then dateDiff(month,dateService,@myDate)<366
    when @mon=7 then dateDiff(month,dateService,@myDate)<32
    when @mon=8 then dateDiff(month,dateService,@myDate)<63
    when @mon=9 then dateDiff(month,dateService,@myDate)<93
    when @mon=10 then dateDiff(month,dateService,@myDate)<124
    when @mon=11 then dateDiff(month,dateService,@myDate)<154
    when @mon=12 then dateDiff(month,dateService,@myDate)<185
    end) group by v.chartID, rs1.procedureKey

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    case just returns a value. you should give a column name to compare

    and mycolumn = (case when @mon=........ end)

  3. #3
    Join Date
    Feb 2003
    Location
    Asheville NC
    Posts
    44
    I'm a bit confused about how giving the case statement a value and a field name would solve the delima? Perhaps the case statement in the where clause isn't possible, but what would you recommend for dealing with this situation?

  4. #4
    Join Date
    Sep 2005
    Posts
    168
    declare @mon as int, @yr as int,
    @myDate as varChar(20) --this could also be datetime
    set @yr=2006
    set @mon=1
    set @mydate='01/31/2006 23:59:59'

    select 0 as DTAP, 0 as DT, 0 as TD, 0 as HIB, 0 as IPV, 0 as MMR, 0 as HEPB, 0 as _VAR, count(v.procedureKey) as FLU, 0 as PPV23, 0 as PCV7, v.chartID, max(rs1.dateService) as dateService from dbo.tbl1 v,
    (select distinct dateService, chartID, procedureKey from fhc.dbo.tbl1 where (datePart(year,dateService)=@yr and datePart(month,dateService)=@mon) and (procedureKey='90657' or procedureKey='90658')) as rs1
    where (v.chartID=rs1.chartID) and (v.procedureKey=rs1.procedureKey) and
    dateDiff(month,dateService,@myDate)< CASE WHEN @mon = 1 THEN 216
    when @mon=2 THEN 244
    when @mon=3 THEN 275
    when @mon=4 THEN 305
    when @mon=5 THEN 336
    when @mon=6 THEN 366
    when @mon=7 THEN 32
    when @mon=8 THEN 63
    when @mon=9 THEN 93
    when @mon=10 THEN 124
    when @mon=11 THEN 154
    ELSE 185 END
    group by v.chartID, rs1.procedureKey

    --HTH--

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •