Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
hello, how can I query dates using "between" function but grouped by months? for example: QUERY:FROM: 15/DIC/2007 TO: 15/FEB/2008RESULT:DECEMBER-2007 --- $49,535JANUARY-2008 --- $45,352FEBRUARY-2008 --- $52.345 Thanks in advance-!
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts
Posted - 2008-05-22 : 11:31:32
Don't use "between" for date range queries.Use >= StartDate and < EndDate+1 day.
select -- First day of month dateadd(month,datediff(month,0,Mydate),0) , sum(MyMoney)from MyTablewhere MyDate >= '20071215' and MyDate < '20080216'group by -- Group by first day of month dateadd(month,datediff(month,0,Mydate),0)order by -- Order by first day of month dateadd(month,datediff(month,0,Mydate),0)