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
 Script file help

Author  Topic 

minimay
Starting Member

2 Posts

Posted - 2015-05-05 : 15:49:36
I am trying to create a script file that will get me the total number of orders in july. How exactly would i say july because i know my syntax is wrong and I would be using sum instead of count right?

Please help this is what i tried

use Cis11101_Northwind
Declare @Julycount int
Set @Julycount= (Select sum(*) From orders Where OrderDate = 'july')
print 'The total orders for july is ' + Cast(@JulyCount as varchar)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-05-05 : 15:59:50
Which July? 2014?

Declare @Julycount int
Set @Julycount = (Select count(*) From orders Where OrderDate >= '07-01-2014' AND OrderDate < '08-01-2014')
print 'The total orders for july is ' + Cast(@JulyCount as varchar)

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

minimay
Starting Member

2 Posts

Posted - 2015-05-05 : 16:06:19
thankyou I ended up doing it this way

Declare @Julycount int
Set @Julycount= (Select Count(*) From orders Where MONTH(OrderDate) = 7)
print 'The total orders for july is ' + Cast(@JulyCount as varchar)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-05-05 : 16:12:11
Your way is not efficient as it can't use an index on OrderDate. Mine can.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2015-05-06 : 04:45:22
As Tara says, on a large table using a function such as MONTH() in the WHERE clause will dramatically slow the query
Go to Top of Page
   

- Advertisement -