SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Using a where clause
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dougancil
Posting Yak Master

USA
217 Posts

Posted - 10/01/2010 :  15:19:24  Show Profile  Send dougancil a Yahoo! Message  Reply with Quote
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

USA
36845 Posts

Posted - 10/01/2010 :  15:25:51  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

USA
217 Posts

Posted - 10/01/2010 :  15:31:05  Show Profile  Send dougancil a Yahoo! Message  Reply with Quote
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

USA
217 Posts

Posted - 10/01/2010 :  15:31:06  Show Profile  Send dougancil a Yahoo! Message  Reply with Quote
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

USA
36845 Posts

Posted - 10/01/2010 :  15:35:16  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

USA
217 Posts

Posted - 10/01/2010 :  15:42:53  Show Profile  Send dougancil a Yahoo! Message  Reply with Quote
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

USA
36845 Posts

Posted - 10/01/2010 :  16:23:17  Show Profile  Visit tkizer's Homepage  Reply with Quote
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

USA
217 Posts

Posted - 10/01/2010 :  16:29:09  Show Profile  Send dougancil a Yahoo! Message  Reply with Quote
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

USA
36845 Posts

Posted - 10/01/2010 :  17:07:59  Show Profile  Visit tkizer's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000