Solved

Dates

Posted on 2011-09-08
2
196 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 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 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