CASE WHEN WHERE clause error

Trying to combine table/column name to t-sql query:
Error:  An expression of non-boolean type specified in a context where a condition is expected, near ')'.

select ....

where
(
   
Case when @prmReportType = 'REPORTDATE' THEN
            + '  workorder.ReportDate >='  + convert(nvarchar(9),@prmStartDate,110)
            + ' and  workorder.ReportDate <=' +   convert(nvarchar(9),@prmEndDate,110)
      
            
      WHEN @prmReportType='ChangeDate' then
            + '  workorder.ChangeDate >='  + convert(nvarchar(9),@prmStartDate,110)
            + ' and  workorder.ChangeDate <=' +   convert(nvarchar(9),@prmEndDate,110)
            
end
)
DonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
Show us the whole SQL statement.

>            + '  workorder.ReportDate >='  + convert(nvarchar(9),@prmStartDate,110)
<wild guess>  Guessing the first + here (and in the 2nd WHEN) needs to be before the whole CASE block.
Lee SavidgeCommented:
Waht are you trying to do? It looks like a mash up of SQL and dynamic SQL. Why are the single quotes around workorder.ChangeDate and workorde.ReportDate and what are the + for?
Jim HornMicrosoft SQL Server Data DudeCommented:
{inspiration from Lee's comment above}
If you are concatenating strings for a SQL statement, dates MUST be delineated by single quote marks, just like in standard T-SQL, so to pull that off, do the below and apply it to all instances with  a date value

+ '  workorder.ReportDate >='''  + convert(nvarchar(9),@prmStartDate,110) + ''''
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Jim HornMicrosoft SQL Server Data DudeCommented:
Exampledate-in-string.jpg
DonAuthor Commented:
The quotes helped, thanks much.

Still not quite there yet....

Select *            
FROM    XYZ

where
(
   
Case when @prmReportType = 'REPORTDATE' THEN
            + '  workorder.ReportDate >=''' + convert(nvarchar(9),@prmStartDate,110) + ''''
            + ' and  workorder.ReportDate <=''' +   convert(nvarchar(9),@prmEndDate,110) +''''
      
            
      WHEN @prmReportType='CHANGEDATE' then
            + '  workorder.ChangeDate >='''  + convert(nvarchar(9),@prmStartDate,110)+''''
            + ' and  workorder.ChangeDate <=''' +   convert(nvarchar(9),@prmEndDate,110)+''''
            
end
)  <-------this is line 114

Msg 4145, Level 15, State 1, Line 114
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Lee SavidgeCommented:
That probably isn't the line. Output your SQL string that you wish to execute and see if you can manually execute it.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You are trying to perform a dynamic condition inside the WHERE. You cannot do it that way, the conditions are fixed. You cannot use "WHERE CASE" either.

You can build the whole SQL as a string with the dynamic parts, and then execute it.
Or you combine the conditions to build proper T-SQL code:
Select *             
FROM    XYZ
where
(
  (@prmReportType = 'REPORTDATE' and
    (    workorder.ReportDate >= convert(nvarchar(9),@prmStartDate,110)
    and  workorder.ReportDate <= convert(nvarchar(9),@prmEndDate  ,110))
   ) OR
   (@prmReportType='CHANGEDATE' and
     (    workorder.ChangeDate >= convert(nvarchar(9),@prmStartDate,110)
     and  workorder.ChangeDate <= convert(nvarchar(9),@prmEndDate  ,110))
   )
)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DonAuthor Commented:
Beautiful,  thank you so very much!!!!!!!!

Hooray!!!!!!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.