Solved

t-sql query

Posted on 2014-01-09
1
299 Views
Last Modified: 2014-01-09
I'm using sql server 2008 R2

I have a test table called TestEmployeeHistory

The table looks like this:

my table
This is the script to create the table.

CREATE TABLE [dbo].[TestEmployeeHistory](
	[Region] [smallint] NULL,
	[SocialNo] [char](9) NULL,
	[EmployeePaidDate] [smalldatetime] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345678 ', CAST(0x8E1302D0 AS SmallDateTime))
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345679 ', CAST(0x8E1302D0 AS SmallDateTime))
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345680 ', CAST(0x8E1302D0 AS SmallDateTime))
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345681 ', CAST(0x8E1302D0 AS SmallDateTime))
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345682 ', CAST(0x912B02D0 AS SmallDateTime))

Open in new window


I have a query that looks like this:

DECLARE @Region varchar(800), @FromDate char(30), @ToDate char(30), @EmployeeSocial char(9)
SET @Region = '1'
SET @FromDate = '07/01/1999'
SET @ToDate = '06/30/2000'
SET @EmployeeSocial = NULL

SELECT h.Region
      ,h.SocialNo
      ,h.EmployeePaidDate
FROM  [TestDatabase].[dbo].[TestEmployeeHistory] h
WHERE
           IF (@Region <> '0')
             BEGIN
	             h.Region IN (RTrim(@Region)) AND 
             END
           If (@EmployeeSocial <> '')
             BEGIN
	             h.SocialNo = @EmployeeSocial AND
             END

GROUP BY h.Region
        ,h.SocialNo
        ,h.EmployeePaidDate

Open in new window


When I run this query I get this error message.

my error message
It seems I am making a mistake in the syntax of my if statements

Anyone where I'm making a mistake in my if statements?
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
1 Comment
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39769605
You can't use "IF" within a query.  You need to use "CASE" instead, or just write the WHERE conditions to do what you need.


WHERE
    ( @Region = 0 OR h.Region IN (RTrim(@Region)) ) AND
    ( @EmployeeSocial = '' OR h.SocialNo = @EmployeeSocial )
GROUP BY h.Region
        ,h.SocialNo
        ,h.EmployeePaidDate
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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 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