Solved

Case Statement in Where Clause on a Parameter

Posted on 2014-11-20
6
213 Views
Last Modified: 2014-11-20
Hello I have the below store proc
declare @po_start_date varchar(15)
declare @po_end_date varchar(15)

SELECT
     ex.vendor_code,
     ex.program,
     ex.Po_num,
     ex.date_create,
     ex.Printed_Date,
     ex.status,
     ex.po_item_No,
     ex.Supplier_pn,
     ex.price,
     ex.unit_of_meas,
     ex.extd_cost,
     ex.quantity,
     ex.open_qty,
     ex.baseline_date,
     ex.[Current_Date],
     ex.nomenclature,
     ex.customer_no,
     ex.control_and_item,
     ex.need_date,
     ex.fax,
     ex.state,
     ex.vendor_name,
     ex.vendor_phone,
     ex.vendor_contact,
     ex.disposition,
     ex.ship_via,
     ex.need_date3,
     ex.a_status,
     ex.data,
     ex.next_higher_assy

FROM
     expeditetbl1 ex 
     left join
(
    select 
        emp_num, pGM_CODES 
    from  exp_dummytbl
    group by emp_num, pGM_CODES
) 
Q ON ex.program = Q.pGM_CODES

where  Q.emp_num='092'
     and ex.vendor_code IS NOT null
     and ex.open_qty <> 0
     and (ex.program ='632'
     and Case  
		When @po_start_date <> ''
	    Then (ex.[Current_Date] > = @po_start_date)
		END 

Open in new window


Iam trying to put a case condition on the param start date to check to see if its empty string or the user has passed a value.Iam getting an incorrect syntax on my code.Please help.
0
Comment
Question by:Star79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40456167
Instead of CASE just use WHERE (what to do if NULL or '' OR expression).
If empty string means return all rows...
SELECT blah, blah, blah
FROM your_table
WHERE (ISNULL(@po_start_date, '') = '' OR ex.[Current_Date] > = @po_start_date)

Open in new window

If empty string means something different, tell us.

btw I have an article out there on SQL Server CASE Solutions, with a wompload of examples, if it helps.
0
 
LVL 20

Expert Comment

by:dsacker
ID: 40456181
First, this is not a stored proc. This is a script.

Secondly, you can't put a command within a CASE statement. This:
Then (ex.[Current_Date] > = @po_start_date)

Open in new window

... is not allowed.

Just guessing, but perhaps your line could read something like:
 and (ex.program ='632'
     and ex.[Current_Date] > CASE
		When ISNULL(@po_start_date, '') = '' Then GETDATE()
		WHEN ISDATE(@po_start_date) = 0 THEN GETDATE()
		ELSE CONVERT(datetime, @po_start_date)
	END

Open in new window

CASE only renders a value. It cannot take action. If you require a @po_start_date, you'll need to validate that at the top of your script (or your proc, if that is what this will eventually become).
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 40456184
Alternatively to Jim's solution..
My guess is that the OP only wants to make the parameter optional.
WHERE
.....
ex.[Current_Date] > = isnull(@po_start_date, ex.[Current_Date] )

Open in new window


Tip: if the @po_start_dateparameter is null then only the > applies, else only the equality applies and annuls the condition predicate.

Hope this helps.
0
Myth Busting: MongoDB Scalability (it scales!)

I was talking with one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. He mentioned to me that several customers have been telling him that “MongoDB doesn’t scale!” MongoDB’s scalability was in question?

My response was, “Is that a joke?"

 
LVL 56

Expert Comment

by:HainKurt
ID: 40456196
here it is

where ex.[Current_Date] >= isnull(@po_start_date, ex.[Current_Date])

oops, it is already posted :)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40456311
Nope, that ISNULL() approach involving a column can be awful for performance.  You're forcing the column value to be compared to itself for every row.

The better coding approach is:
   and (ISNULL(@po_start_date, '') = '' OR @po_start_date = ex.[Current_Date])
because SQL can, and very often will, 'short-circuit' before the last comparison.


For example, try the code below against any table with a "name" column, and look at the query plans.  For the first plan, you won't (shouldn't) see a 'Predicate'/comparison at all.  With the second, you'll see that it does need a Predicate/comparison.


declare @name varchar(128)

--no predicate, because SQL can strip it.
select *
from dbo.table_with_name_column
where isnull(@name, '') = '' or name = @name --proper method

--with predicate, because SQL can't strip it.
select *
from dbo.table_with_name_column
where name = isnull(@name, name) --improper method, do not use!
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40456541
agree with ScottPletcher

and (ISNULL(@po_start_date, '') = '' OR @po_start_date = ex.[Current_Date])

or this one

and (ISNULL(@po_start_date, '') = '' OR @po_start_date = ex.[Current_Date])

^^^ looks best option, which I always used in my old asp portals :) after getting values from a search form on a webpage, I used one query like

sql="select ...
where ...
((':param1'='') or (column1=':param1')) and
((':param2'='') or (column2=':param2')) and
...
((':paramN'='') or (columnN=':paramN'))"

then

sql.replace(":param1",param1)
sql.replace(":param2",param2)
...
sql.replace(":paramN",paramN)

then run sql in db...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question