Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Using a where clause

Author  Topic 

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-01 : 15:19:24
I have the following query:

SELECT [Employees].name, [Employees].EmployeeNumber, dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440 +1 as date,
dateadd(n,Timestamp,'12/31/1899')-([LoggedIn]/1000)/60/1440+1 AS Day, Sum([LoggedIn]/1000/60) AS LogIn,
Sum ([OnTime]/1000/60) AS OnTime1,
dateadd (n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1 AS Da INTO ScratchPad1

FROM Employees INNER JOIN dbo.mOpInterval ON [Employees].Employeenumber = dbo.mOpInterval.Opname

GROUP BY [Employees].name, [Employees].EmployeeNumber, dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1,
dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1,dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1

where dateadd (n,Timestamp,'12/31/1899')-([LoggedIn]/1000)/60))/1440+1, Between 9/15/2010 And 9/18/2010


And when I try to run this I get the following error:
Server: Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'where'.

Can anyone assist me with what I'm not seeing here?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-01 : 15:25:51
You need single quotes around the date in the WHERE clause, and you've got a comma in there that shouldn't be.

I think you are also missing parenthesis for your DATEADD calculations. I don't have SQL Server access right now, but I think you've got quite a few syntax problems.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-01 : 15:31:05
tara,

Thank you and I've gone back and made some changes but it seems that the incorrect syntax is before the where clause:

SELECT [Employees].name, [Employees].EmployeeNumber, dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440 +1 as date,
dateadd(n,Timestamp,'12/31/1899')-([LoggedIn]/1000)/60/1440+1 AS Day, Sum([LoggedIn]/1000/60) AS LogIn,
Sum ([OnTime]/1000/60) AS OnTime1,
dateadd (n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1 AS Da INTO ScratchPad1

FROM Employees INNER JOIN dbo.mOpInterval ON [Employees].Employeenumber = dbo.mOpInterval.Opname

GROUP BY [Employees].name, [Employees].EmployeeNumber, dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1,
dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1,dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1

where dateadd ((n,Timestamp,'12/31/1899')-([LoggedIn]/1000)/60))/1440+1) Between '9/15/2010' And '9/18/2010'

produces this error:
Server: Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'where'.
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-01 : 15:31:06
tara,

Thank you and I've gone back and made some changes but it seems that the incorrect syntax is before the where clause:

SELECT [Employees].name, [Employees].EmployeeNumber, dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440 +1 as date,
dateadd(n,Timestamp,'12/31/1899')-([LoggedIn]/1000)/60/1440+1 AS Day, Sum([LoggedIn]/1000/60) AS LogIn,
Sum ([OnTime]/1000/60) AS OnTime1,
dateadd (n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1 AS Da INTO ScratchPad1

FROM Employees INNER JOIN dbo.mOpInterval ON [Employees].Employeenumber = dbo.mOpInterval.Opname

GROUP BY [Employees].name, [Employees].EmployeeNumber, dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1,
dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1,dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1

where dateadd ((n,Timestamp,'12/31/1899')-([LoggedIn]/1000)/60))/1440+1) Between '9/15/2010' And '9/18/2010'

produces this error:
Server: Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'where'.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-01 : 15:35:16
I'd recommend counting the number of parenthesis that you've got to ensure you have an equal number of lefts and rights.

I think you've got an issue with the join condition too.

When I get back to a SQL Server connection, I'll see if I can help out more.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-01 : 15:42:53
Tara,

I'll let you know that if I run this without the where clause, this query produces results, just not in a specific date range but for all data within the database, so I know that the inner join works correctly as is (I believe) and I'll verify the number of parenthesis to make sure I don't have too many in there.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-01 : 16:23:17
Two problems that I can find now that I'm in front of SQL Server:
1. Your WHERE clause is not in the correct location (needs to be before the GROUP BY).
2. You've got 3 left parens and 5 right parens in the WHERE clause.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-01 : 16:29:09
Tara,

Thanks for your help. This works now:

SELECT [Employees].name, [Employees].EmployeeNumber, dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440 +1 as date,
dateadd(n,Timestamp,'12/31/1899')-([LoggedIn]/1000)/60/1440+1 AS Day, Sum([LoggedIn]/1000/60) AS LogIn,
Sum ([OnTime]/1000/60) AS OnTime1,
dateadd (n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1 AS Da INTO ScratchPad1

FROM Employees INNER JOIN dbo.mOpInterval ON [Employees].Employeenumber = dbo.mOpInterval.Opname

where dateadd (n,Timestamp,'12/31/1899')-([LoggedIn]/1000)/60/1440+1 Between '9/15/2010' And '9/18/2010'

GROUP BY [Employees].name, [Employees].EmployeeNumber, dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1,
dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1,dateadd(n,Timestamp, '12/31/1899')-([LoggedIn]/1000)/60/1440+1
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-01 : 17:07:59
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -