Solved

SQL where clause problem

Posted on 2013-12-20
6
409 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
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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 59

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now