Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

t-sql query

Posted on 2014-01-23
2
Medium Priority
?
108 Views
Last Modified: 2014-01-25
Hi experts,

I'm using sql server 2008 R2.

I have a query where in the WHERE clause I have the following if statements.

SELECT * 
FROM MyTaBLE h, MyOtherTable d
WHERE
          If (@Region <> '0')
              begin
                     h.Region in (RTrim(@Region)) AND
              end
          If (@EmployeeSocial <> '')
              begin
                     h.EmployeeSocial = @EmployeeSocial AND
            end
      h.Region = d.Region

Open in new window


Is there a better way to write the if statements that are in my WHERE clause without using if statements?

FYI, my parameters are of these datatypes:

@Region varchar(800), @EmployeeSocial char(9)
0
Comment
Question by:maqskywalker
[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
  • 2
2 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 39805366
Something like this perhaps:
SELECT  *
FROM    MyTaBLE h
        INNER JOIN MyOtherTable d ON h.Region = d.Region
WHERE   (@Region <> '0' AND h.Region = RTRIM(@Region))
        AND (@EmployeeSocial <> '' AND h.EmployeeSocial = @EmployeeSocial)

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39809488
Incidentally you should not need to do a RTRIM() .  Typically the following are always true:
'Anthony' = 'Anthony       '

If you are bound and determined to use RTIM() than do this:
SET @Region = RTRIM(@Region)
SELECT  *
FROM    MyTaBLE h
        INNER JOIN MyOtherTable d ON h.Region = d.Region
WHERE   (@Region <> '0' AND h.Region = @Region)
        AND (@EmployeeSocial <> '' AND h.EmployeeSocial = @EmployeeSocial)

Open in new window

0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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…
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.

722 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