Solved

Between date range question

Posted on 2008-10-01
7
316 Views
Last Modified: 2010-03-19
I am using this as an example:

BETWEEN GETDATE() - 14 AND GETDATE()

I would like to pull data that is between 9/1/2008 and 14 days prior to today.  How do I need to change the above code to make that work?
0
Comment
Question by:binaryman101
  • 3
  • 2
  • 2
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
what about this:

WHERE yourfield >= DATEADD(MONTH, -1, CONVERT(VARCHAR(10), CONVERT(DATETIME, GETDATE(), 120), 120) ) 
  AND yourfield >= DATEADD(DAY, -14, CONVERT(VARCHAR(10), CONVERT(DATETIME, GETDATE(), 120), 120) ) 

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
sorry, second >= should be <

WHERE yourfield >= DATEADD(MONTH, -1, CONVERT(VARCHAR(10), CONVERT(DATETIME, GETDATE(), 120), 120) ) 
  AND yourfield < DATEADD(DAY, -14, CONVERT(VARCHAR(10), CONVERT(DATETIME, GETDATE(), 120), 120) ) 

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
This is what you want.

select * from YourTable
where [Date] >= convert(datetime,convert(varchar(11),getdate()-14,101),101)
and [Date] < convert(datetime,convert(varchar(11),getdate(),101),101)
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:binaryman101
Comment Utility
9/1/2008 will never change but the 14 days from now will.  Is the above solutions still applicable?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
select * from YourTable
where [Date] >= convert(datetime, '09/01/2008', 1)
and [Date] <= convert(datetime,convert(varchar(11),getdate()-14,101),101)
0
 

Author Comment

by:binaryman101
Comment Utility
I get the following message:

Error Mesage:  Conversion failed when converting datefime from character string
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 125 total points
Comment Utility
Sorry... first should have been 101 as well.
...


select * from YourTable
where [Date] >= convert(datetime, '09/01/2008', 101)
and [Date] <= convert(datetime,convert(varchar(11),getdate()-14,101),101)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now