?
Solved

Dates

Posted on 2011-09-08
2
Medium Priority
?
199 Views
Last Modified: 2012-06-22
I have been using the following date ranges in queries for several months now, using SQL 2008 R2

declare @range varchar(20), @start datetime, @end datetime

if @range='Today'
   select @start= floor(CONVERT(float,getdate())), @end=getdate()
else if @range='Yesterday'
   select @end= floor(CONVERT(float,getdate())), @start=@end-1
else if @range='Last 5 Days'
   select @end= floor(CONVERT(float,getdate())), @start=@end-5
else if @range='Last 7 Days'
   select @end= floor(CONVERT(float,getdate())), @start=@end-7
else if @range='Month To Date'
   select @end=GETDATE(), @start= DATEADD(d,1-DAY(getdate()),floor(CONVERT(float,getdate())))
else if @range='Last Month'
   select @end= DATEADD(d,1-DAY(getdate()),floor(CONVERT(float,getdate()))), @start=DATEADD(m,-1,@end)

The query is usually

select etc
from ect
where fielddate between @start and @end

My questions is in two parts

- Is the way I am doing correct/efficient or is there a better way of doing this?
- I also need the date ranges for Last Full Week - in this case Mon - Sun, Last Full Year, Year to Date

Thanks
0
Comment
Question by:halifaxman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 29

Accepted Solution

by:
QPR earned 2000 total points
ID: 36501203
I'm confused as to why you are converting the dates to a floor.
Why aren't you treating date variables as date data types?

There are some examples of getting date values/ranges here
http://blog.sqlauthority.com/2008/08/29/sql-server-few-useful-datetime-functions-to-find-specific-dates/
0
 
LVL 29

Expert Comment

by:QPR
ID: 36501206
umm I meant converting to a float not a floor
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question