Solved

SQL where clause problem

Posted on 2013-12-20
6
407 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
Comment Utility
...
WHERE
   CASE @parameter
      WHEN 0 THEN someDateColumn < @YearEnd
      ELSE someDateColumn > @YearEnd
   END
0
 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
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
Comment Utility
WHERE (
        parameter  = 0 and
             datecol < @yearend
        ) 
        OR 
       (
           parameter   <> 0 and
             datecol > @yearend
      )

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

771 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

14 Experts available now in Live!

Get 1:1 Help Now