Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL where clause problem

Posted on 2013-12-20
6
Medium Priority
?
424 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 1000 total points
ID: 39732081
WHERE (
        parameter  = 0 and
             datecol < @yearend
        ) 
        OR 
       (
           parameter   <> 0 and
             datecol > @yearend
      )

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

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 1000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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
Suggested Courses

810 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