[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Dates

Posted on 2011-09-08
2
Medium Priority
?
201 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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