Solved

t-sql query

Posted on 2014-01-23
2
103 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
  • 2
2 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
Comment Utility
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
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

743 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

17 Experts available now in Live!

Get 1:1 Help Now