Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Case in a Where clause of stored procedure

Status
Not open for further replies.

Tomadams

Programmer
Jun 26, 2001
141
US
I have a situation where my customer wants to run a report by any one of 4 dates. I have this in my where clause:
and (case when @DateType = 'Date1' then ([filename].[Date1] between @StartDate and @StopDate)
when @DateType = 'Date2' then ([filename].[Date2] between @StartDate and @StopDate)
when @DateType = 'Date3' then ([filename].[Date3] between @StartDate and @StopDate)
when @DateType = 'Date4' then ([filename].[Date4] between @StartDate and @StopDate)
end)

the error I get is:
Msg 156, Level 15, State 1, Procedure sp_RamAccessSummary, Line 72
Incorrect syntax near the keyword 'between'.

The first Between in line 72

Any idea what I am doing here??? thanks in advance
 
Code:
AND (
(@DateType = 'Date1' AND ([filename].[Date1] BETWEEN @StartDate AND @StopDate))
OR
(@DateType = 'Date2' AND ([filename].[Date2] BETWEEN @StartDate AND @StopDate))
OR
(@DateType = 'Date3' AND ([filename].[Date3] BETWEEN @StartDate AND @StopDate))
OR
(@DateType = 'Date4' AND ([filename].[Date4] BETWEEN @StartDate AND @StopDate))

)
 
or like this

Select ....
from filename
where @DateType = 'Date1' AND [filename].[Date1] BETWEEN @StartDate AND @StopDate
Union
Select ....
from filename
where @DateType = 'Date2' AND [filename].[Date2] BETWEEN @StartDate AND @StopDate
Select ....
from filename
where @DateType = 'Date3' AND [filename].[Date3] BETWEEN @StartDate AND @StopDate
Union
Select ....
from filename
where @DateType = 'Date4' AND [filename].[Date4] BETWEEN @StartDate AND @StopDate
 
Gotit to work this way:
(case
when @DateType = 'Date1' then ([filename].[Date1]
when @DateType = 'Date2' then ([filename].[Date2]
when @DateType = 'Date3' then ([filename].[Date3]
when @DateType = 'Date4' then ([filename].[Date4]
end)
between @StartDate and @StopDate


Thanks folks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top