Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Case Statement in Where Clause on a Parameter

Posted on 2014-11-20
6
Medium Priority
?
215 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 2000 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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
LVL 59

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 70

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 59

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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