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
 General SQL Server Forums
 New to SQL Server Programming
 Today's Date

Author  Topic 

rnelsch
Starting Member

31 Posts

Posted - 2012-09-11 : 13:12:17
Trying to use the following and I keep getting a syntax error

Select Job.Department,
Sum(Job.TotalTicket)
From Job
Group By Job.Department
Where Job.SchedDate Between DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0)AND DATEADD(d, DATEDIFF(d, 0, GETDATE()), 1))

I swear I've used this before and it worked just fine to pull data for Today's Date.

Ryan A Nelsch

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-09-11 : 13:17:58
You have an extra parenthesis and you need to move the GROUP BY after the WHERE clause.:
elect Job.Department,
Sum(Job.TotalTicket)
From Job
Where Job.SchedDate Between DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0)AND DATEADD(d, DATEDIFF(d, 0, GETDATE()), 1))
Group By Job.Department
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-11 : 14:19:57
other small issue is using BETWEEN will cause it include records that got created on midnight next day as well

so if you really want result to be restricted to today much safer way would be

..
Where Job.SchedDate >=DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0)
AND Job.SchedDate < DATEADD(d, DATEDIFF(d, 0, GETDATE()), 1)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rnelsch
Starting Member

31 Posts

Posted - 2012-09-11 : 16:10:28
Ah, thank you.

Ryan A Nelsch
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-11 : 16:22:35
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -