473,395 Members | 1,762 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,395 software developers and data experts.

WHERE clause with IN clause with CASE WHEN...

I am trying to use below code and it does not work... but cant live without below conditions... is there any otherway anyone knows to do the same thing?

if i pass 0 in STRID then it should show me ALL records but if i pass 1,2,5 then it should show me only records which has 1,2,5 ID's in tble.

Thanks in Advance..

DECLARE @STRID AS VARCHAR(MAX)
--SET @STRID = '1,2,3'
SET @STRID = NULL

SELECT * FROM STORES
WHERE StrID IN ( CASE WHEN @STRID = '0' THEN @STRID WHEN @STRID <> '0' THEN (SELECT * FROM FN_SPLIT_ID_STR(@STRID,',')) END )
Feb 13 '12 #1
4 1923
ck9663
2,878 Expert 2GB
First, instead of the second when, use an ELSE.
Second, this
Expand|Select|Wrap|Line Numbers
  1. CASE WHEN @STRID = '0' THEN @STRID
  2.  
should be
Expand|Select|Wrap|Line Numbers
  1. CASE WHEN @STRID = '0' THEN STRID
  2.  
I'm not digging into your function.

Happy Coding!!!


~~ CK
Feb 13 '12 #2
Sorry that was typo, i have added else but the code suggested also did not work for me...

may be i was not clear in description ... i have changed the query little (below)...

what i need is if parameter value is 0(ZERO) then all records should display and if the parameter value is passed as (multiple ID's) then it should display only the selected records...

Please suggest...

Expand|Select|Wrap|Line Numbers
  1. DECLARE @STRID AS VARCHAR(MAX)
  2. SET @STRID = '1,2,3'
  3. --SET @STRID = '0'
  4.  
  5. SELECT * FROM STORES 
  6. WHERE (CASE WHEN @STRID = '0' THEN @STRID ELSE Store_ID END)  
  7. IN  
  8. ( CASE WHEN @STRID = '0' THEN @STRID ELSE (SELECT *
Feb 13 '12 #3
ck9663
2,878 Expert 2GB
Use the code tag so it's readable.

I'm sure this can be refactored, but if your table is not that big, it will not matter.

Try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM STORES 
  2. WHERE 
  3.    (isnull(@STRID,'0') = '0' and store_id = stored_id) or
  4.    (isnull(@STRID,'0') <> '0' and store_id in (SELECT * FROM FN_PLGBA_SPLIT_CSV(@STRID,',')))
  5.  
Only one of those condition will be true at any given point in time. I considered NULL as '0'. Just change it if necessary.

Happy Coding!!!


~~ CK
Feb 14 '12 #4
It worked like a charm.... Thank You so much..
Feb 14 '12 #5

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

Similar topics

3
by: Matik | last post by:
Hello all, I belive, my problem is probably very easy to solve, but still, I cannot find solution: declare @i int declare @z int create table bubusilala (
10
by: Jozef de Veuster | last post by:
Hi, I'm trying to create a Stored Procedure that returns a recordset, but I want to be able to choose the ORDER BY clause in mijn parameter list of the Stored Procedure. Since CASE .. WHEN can...
5
by: DaaaDaaa | last post by:
Hi, Here's what I'd like to do: table (Orders) has fields of Processed, Paid (both of boolean yes|no) etc, I want to return a count of of Total number of Orders, Number of Processed vs. Number...
1
by: barrathi | last post by:
Hi all i need one query help! for reporting purpose i wrote one query - follows select s.tran_empid as EmpId, e.M_EMPL_NAME as EmpName, p.PROJ_NAME as Project, t.TITL_NAME as Title,CASE WHEN...
2
by: Vinnie123 | last post by:
Is it possible to have mysql ignore the case when performing a search? I am making a user signup script. I want the usernames to be case sensitive (i.e. I do not want to store only lowercase...
1
amitpatel66
by: amitpatel66 | last post by:
Hi, Is PLSQL Anonymous block allowed in CASE WHEN statement when CASE is used in SELECT statement. Check below code which executes but does not return anything: SELECT job, CASE WHEN job =...
4
by: laurenquantrell | last post by:
Is there an equivalant construction to the CASE WHEN statement that can be used in the WHERE clause? For example, this works: SELECT FirstName = CASE WHEN c.FirstName = 'Bob' THEN 'Robert'...
2
by: philip | last post by:
hello i am learning how to write stored procedures in sql server, i would like to know what's wrong with the following statement? please help the management studio gives me the following...
4
by: NDayave | last post by:
Hi, I have a query that is sorting people into their respective areas of the country and need to assign each postcode the correct label (Eg: 'North', 'Wales', etc.). I am trying to do this in a...
1
by: Charles Ndethi | last post by:
Hi I want to set the session id to a value after evaluating the request_time using a case when statement. here 's the table userinput_id session_id request_time ...
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: 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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.