Solved

SQL where clause problem

Posted on 2013-12-20
6
416 Views
Last Modified: 2013-12-20
Hi

I have a parameter passed into a stored proc. If its a 0 I want a data less than a fixed date (called @YearEnd) and if its 1 I want data greater than @yearend

How do I do this, have been playing with case statements in the where clause, but getting a bit stuck.

Andy
0
Comment
Question by:Andy Green
[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 34

Expert Comment

by:Brian Crowe
ID: 39732058
...
WHERE
   CASE @parameter
      WHEN 0 THEN someDateColumn < @YearEnd
      ELSE someDateColumn > @YearEnd
   END
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39732071
You can't in the way you want.

Either you do:

if @myparam = 0
begin
-- select statement here with one where clause
end
else
begin
-- a different select statement with another where clause
end

Open in new window


of you use dynamic sql

declare @sql nvarchar(1000), @option nvarchar(1), @yearend datetime

select @yearend = getdate() -- example value

if @myparam = 0
  select @option = '<'
else
  select @option = '>'

select @sql = 'select * from mytable where myvar ' + @option + ' ' + @yearend

exec (@sql)

Open in new window

0
 
LVL 9

Accepted Solution

by:
QuinnDex earned 250 total points
ID: 39732081
WHERE (
        parameter  = 0 and
             datecol < @yearend
        ) 
        OR 
       (
           parameter   <> 0 and
             datecol > @yearend
      )

Open in new window

0
Industry Leaders: 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!

 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39732098
Is the query complicated? If not you could of course simply do

IF @param=0
  SELECT ... WHERE datefield<@yearend
ELSE
  SELECT ... WHERE datefield>@yearend

The CASE solution is also possible and of course is more elegant, as you don't duplicate the query itself and don't have to maintain both queries and keep them in sync aside of the < or >.

But you can't put datefield>@yearend as a THEN or ELSE expression, you can compute two different expressions, which result in >0, eg:

SELECT.... WHERE CASE WHEN @passedinparam=0
    THEN datediff(d, datefield, @yearend)
    ELSE datediff(d, @yearend, datefield)
    END >0

You didn't specify the datefield name and the name if the parameter being 0 or 1, you can figure that out, I assume.

Bye, Olaf.
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
ID: 39732099
I agree with QuinnDex but note the first suggestion can be done like this:

WHERE
   CASE
      WHEN @parameter = 0 AND someDateColumn < @YearEnd THEN 1
      WHEN @parameter <> 0 AND someDateColumn > @YearEnd THEN 1
      ELSE 0
   END = 1

You will want to check the performance of the different solutions and pick what works best for you.
0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 39732150
Thanks Guys

I have split the points I went with QuinnDex in the first instance, it worked but I ran into problems with adding further parameters, I was able to do this easily with mwvisa's solution.

I know I didn't mention the other params in my OP but I didn't want to over complicate things
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

739 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