Avatar of maqskywalker
maqskywalker
 asked on

t-sql query

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?
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck