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.
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 intSet @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 KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
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) |
|
|
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 KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
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 |
|
|
|
|
|