Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Between date range question

Posted on 2008-10-01
7
Medium Priority
?
368 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
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22617435
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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22617443
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
ID: 22617456
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:binaryman101
ID: 22617503
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
ID: 22617520
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
ID: 22617669
I get the following message:

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

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22617695
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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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