473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

CASE in WHERE clause?

gateshosting
I thought this syntax was correct, but I can't figure out why it won't work. Maybe it just is not supported?
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE spTEST @includepaid bit AS
  2.  
  3. SELECT FIELD FROM TABLE
  4. WHERE
  5.   SOLD = 1
  6.   AND CUSTOMER = 2
  7.  
  8.   CASE
  9.     WHEN @includepaid = 1 THEN AND [Paid] = 1
  10.   END
  11.  
This is not my exact query, but you get the drift. All my logic is correct in my SP. I want to be able to turn PAID to include or not include...

Thanks,

Michael C. Gate
Dec 6 '06 #1
2 4808
almaz
168 Expert 100+
I thought this syntax was correct, but I can't figure out why it won't work. Maybe it just is not supported?

...

This is not my exact query, but you get the drift. All my logic is correct in my SP. I want to be able to turn PAID to include or not include...

Thanks,

Michael C. Gate
CASE statement can only return a value, not a condition string. So you should rewrite your WHERE clause like following (assuming that Paid column doesn't allow nulls):
Expand|Select|Wrap|Line Numbers
  1. WHERE
  2.   SOLD = 1
  3.   AND CUSTOMER = 2
  4.   AND [Paid] =
  5.     CASE
  6.       WHEN @includepaid = 1 THEN 1
  7.       ELSE [Paid]
  8.     END
Dec 6 '06 #2
Thanks... sorry it was a topic already covered. I found that later doing some searches.

I understand now how it only returns a value. I am thinking like VB or C#, and not SQL. I am new to the logic of SQL. Thanks,

Michael C. Gates
Dec 6 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Largo SQL Tools | last post by:
Can anyone tell me if it's possible to use a Case statement in a Where clause, and if so, the proper syntax? J.R. Largo SQL Tools The Finest Collection of SQL Tools Available...
3
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
7
by: mandible | last post by:
Hello I'm trying to have some control on how my data is ordered depending on an input parameter my question is in a stored procedure how can I do something like this at the end of my...
5
by: Sascha.Moellering | last post by:
Hi, I receive the error code SQL0338N if I try to compile this statement (part of the statement): .... left outer join lateral (SELECT * FROM LIZSYSABA.VWZL0359TBS WHERE tbs_name = CASE WHEN...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.