Solved

SQL where clause problem

Posted on 2013-12-20
6
417 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

726 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