Solved

Calculating 7 days ahead of currentdate?

Posted on 2006-10-30
6
479 Views
Last Modified: 2008-02-26
Hi,

I am trying to create a query that will give me all the records that have a date equal to currentDate + 7, so all records that have a date between the current date and 1 week basically.

My sql seems to be a bit off here

SELECT m.*, c.fname + ' ' + c.lname as fullname, cat.category as categoryName
FROM table m
INNER JOIN categories cat ON cat.categoryId = m.categoryId
LEFT JOIN customer c ON c.customerId = m.customerId
WHERE DATEADD(d,7,CONVERT(datetime,CONVERT(varchar(15),GETDATE(),101))) >= myDateColumn

Any help appreciated.  I am looking for all records that have a value in the 'myDateColumn' that is between the currentDate and 1 week.  I am not interested in time at this point as we are only storing the date in the db.

-ws

0
Comment
Question by:Westside2004
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17836530
where mydatecolumn between getdate() and getdate()+7

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 125 total points
ID: 17836546
WHERE myDateColumn BETWEEN CONVERT(CHAR(8), GETDATE(), 112) AND DATEADD(DAY, 7, CONVERT(CHAR(8), GETDATE(), 112))


You may need to change 7 to 6, depending on specifically how you want it to work.
0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17836589
note ... this will not get a record that happened "today" but 5 minutes ago... if you want everything from midnight now to 11:59:59 pm of the day a week from today, you truncate the date ...

where mydatecolumn>=convert(datetime, convert(varchar,getdate(),1)) and mydatecolumn < convert(datetime, convert(varchar,getdate()+8,1))

This approach (going ahead an extra day but saying less than instead of less-than-or-equal) has the advantage of not manipulating your datecolumn, which makes it faster (manipulating a column can keep the optimizer from using an index on it)
0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 10

Expert Comment

by:AaronAbend
ID: 17836600
sorry - to clarify - in my second comment, "this" was referring to my original post, not Scott's comment.
0
 
LVL 1

Author Comment

by:Westside2004
ID: 17836982
HI,

Yes, that worked.  Thanks for the quick responses.

-ws
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 17837079
>> I am not interested in time at this point as we are only storing the date in the db. <<

Btw, if the column type is "datetime" or "smalldatetime", SQL *is* storing the time -- if will default to midnight unless otherwise specified.  That's why I stripped the time, using the CONVERTs, in my code.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

713 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