Solved

t-sql query

Posted on 2014-01-23
2
107 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 500 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

617 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