RadhaKrishnaKiJaya
asked on
Need help with query
Hi Expert,
This is my query in the stored procedure. When I execute it gives me error 'Invalid syntax near AND'. Any idea what is wrong.
Thanks in advance.
DECLARE @FromDate1 nchar(10)=NULL;
DECLARE @ToDate1 nchar(10)=NULL;
DECLARE @Classification1 nchar(1)=NULL;
SET @FromDate1 = @FromDate;
SET @ToDate1 = @ToDate;
SET @Classification1= @Classification;
SET @SQL='SELECT
U.[UserID]
,ltrim(rtrim(U.[ClientUser ID])) as ClientUserID
,ltrim(rtrim(U.[UserFirstN ame])) as UserFirstName
,ltrim(rtrim(U.[UserLastNa me])) as UserLastName
,ltrim(rtrim(U.[UserAdd1]) ) as UserAdd1
,ltrim(rtrim(U.[UserAdd2]) ) as UserAdd2
,ltrim(rtrim(U.[UserCity]) ) as UserCity
,ltrim(rtrim(s.[StateName] )) as State
,ltrim(rtrim(U.[UserZip])) as UserZip
,ltrim(rtrim(U.[UserPhone] )) as UserPhone
,ltrim(rtrim(U.[UserEmail] )) as UserEmail
,ltrim(rtrim(F.StsDescript ion)) as UserType
,ltrim(rtrim(U.[UserActive ])) as UserActive
,ltrim(rtrim(U.[UserStartD ate])) as UserStartDate
,ltrim(rtrim(FP.[StsDescri ption])) as Classification
,ltrim(rtrim(D.[DayName])) as AttendenceDay
,U.[TodaysDate],U.[Attende nceDay],U. [UserState ],U.[UserT ype],U.[Cl assificati on]
FROM [USERDETAILS] as U
LEFT OUTER JOIN DAYMST As D on D.DayID =U.AttendenceDay
LEFT OUTER JOIN STATESMST As S on S.StateID =U.UserState
LEFT OUTER JOIN FPSCODES as F on F.StsDescription_Code=U.Us erType and F.StatusCode =''UT''
LEFT OUTER JOIN FPSCODES as FP on FP.StsDescription_Code=U.C lassificat ion and FP.StatusCode =''CLF''
where U.[UserActive] =''Y'''
if @FromDate1 <> ''
begin
SET @SQL = @SQL + ' and CONVERT(DATETIME, RIGHT(UserStartDate, 4) + LEFT(UserStartDate, 4), 112) >=' + CAST(@FromDate1 AS NVARCHAR(10));
end
if @ToDate1 <> ''
begin
SET @SQL = @SQL + ' and CONVERT(DATETIME, RIGHT(UserStartDate, 4) + LEFT(UserStartDate, 4), 112) <= ' + CAST(@ToDate1 AS NVARCHAR(10));
end
if @Classification1 <> ''
begin
SET @SQL = @SQL + ' and U.[Classification] =' + CAST(@Classification1 AS NVARCHAR(1));
end
SET @SQL = @SQL + ' order by UserFirstName'
EXEC(@SQL)
This is my query in the stored procedure. When I execute it gives me error 'Invalid syntax near AND'. Any idea what is wrong.
Thanks in advance.
DECLARE @FromDate1 nchar(10)=NULL;
DECLARE @ToDate1 nchar(10)=NULL;
DECLARE @Classification1 nchar(1)=NULL;
SET @FromDate1 = @FromDate;
SET @ToDate1 = @ToDate;
SET @Classification1= @Classification;
SET @SQL='SELECT
U.[UserID]
,ltrim(rtrim(U.[ClientUser
,ltrim(rtrim(U.[UserFirstN
,ltrim(rtrim(U.[UserLastNa
,ltrim(rtrim(U.[UserAdd1])
,ltrim(rtrim(U.[UserAdd2])
,ltrim(rtrim(U.[UserCity])
,ltrim(rtrim(s.[StateName]
,ltrim(rtrim(U.[UserZip]))
,ltrim(rtrim(U.[UserPhone]
,ltrim(rtrim(U.[UserEmail]
,ltrim(rtrim(F.StsDescript
,ltrim(rtrim(U.[UserActive
,ltrim(rtrim(U.[UserStartD
,ltrim(rtrim(FP.[StsDescri
,ltrim(rtrim(D.[DayName]))
,U.[TodaysDate],U.[Attende
FROM [USERDETAILS] as U
LEFT OUTER JOIN DAYMST As D on D.DayID =U.AttendenceDay
LEFT OUTER JOIN STATESMST As S on S.StateID =U.UserState
LEFT OUTER JOIN FPSCODES as F on F.StsDescription_Code=U.Us
LEFT OUTER JOIN FPSCODES as FP on FP.StsDescription_Code=U.C
where U.[UserActive] =''Y'''
if @FromDate1 <> ''
begin
SET @SQL = @SQL + ' and CONVERT(DATETIME, RIGHT(UserStartDate, 4) + LEFT(UserStartDate, 4), 112) >=' + CAST(@FromDate1 AS NVARCHAR(10));
end
if @ToDate1 <> ''
begin
SET @SQL = @SQL + ' and CONVERT(DATETIME, RIGHT(UserStartDate, 4) + LEFT(UserStartDate, 4), 112) <= ' + CAST(@ToDate1 AS NVARCHAR(10));
end
if @Classification1 <> ''
begin
SET @SQL = @SQL + ' and U.[Classification] =' + CAST(@Classification1 AS NVARCHAR(1));
end
SET @SQL = @SQL + ' order by UserFirstName'
EXEC(@SQL)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I just came across a previous related question
https://www.experts-exchange.com/questions/28961789/Need-help-with-a-query.html
Your column [UserStartDate] is a string (char or varchar) in mmddyyyy format
Storing dates as strings always a bad idea and I suggest you look at this:
https://www.experts-exchange.com/articles/1499/DATE-and-TIME-don't-be-scared-and-do-it-right-the-first-time.html
https://www.experts-exchange.com/questions/28961789/Need-help-with-a-query.html
Your column [UserStartDate] is a string (char or varchar) in mmddyyyy format
Storing dates as strings always a bad idea and I suggest you look at this:
https://www.experts-exchange.com/articles/1499/DATE-and-TIME-don't-be-scared-and-do-it-right-the-first-time.html
ASKER
Thank you very much everybody for helping me.
Otherwise we're guessing with a lot of code..
Also, throw in a SELECT @SQL and post that in this question. Might make the error obvious.