We help IT Professionals succeed at work.

2 dates in a SQL stored proc - 7 days prior and 30 days in future

71 Views
Last Modified: 2019-03-06
Hi,

I have a new for 2 dates in my SQL stored procedure.  
1) 7 Days prior to today
2) 30 days in the future from today.

I am using these, but they don't seem to be working:
      DECLARE @7daysago DATE = CONVERT(DATE, DATEADD(DAY, -DAY(GETDATE()) -7, DATEADD(MONTH, 1, GETDATE())));
      DECLARE @30daysinfuture DATE = CONVERT(DATE, DATEADD(DAY, -DAY(GETDATE()) +30, DATEADD(MONTH, 1, GETDATE())));

Could anyone please point me in the right direction?

cheers
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
that is perfect.  Many thanks!
CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019

Commented:
Glad to help.

If you want a slightly different way that is probably more mainstream:
select getdate() as today, dateadd(day,-7,getdate()) as seven_days_ago, dateadd(day,30,getdate()) as thirty_days_from_now
PortletPaulEE Topic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2014
Awarded 2013

Commented:
be careful using getdate() because it is actually the current date AND time, you can use cast(getdate() as date) to set the time to 00:00:00

select
  cast(getdate() as date) as today
, cast(getdate() as date) -7  as seven_days_ago
, cast(getdate() as date) +30 as thirty_days_from_now

Using integers for addition isn't common in TSQL coding, instead  using dateadd() is more common, i.e.

select
  cast(getdate() as date) as today
, dateadd(day,-7,cast(getdate() as date) ) as seven_days_ago
, dateadd(day,30,cast(getdate() as date) ) as thirty_days_from_now

These will also work:

 CONVERT(DATE, DATEADD(DAY, -7, GETDATE()))
 CONVERT(DATE, DATEADD(DAY, +30, GETDATE()))

[edit]
looking back at your originals it seems that you felt it necessary to alter the day and the month - individually - when using DATEADD(). Hopefully you can see that this isn't needed, dateadd() will accurately add or deduct the number of time units from a parameter datetime value.

If in doubt on how to use a function, books online will help: e.g.
https://docs.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-2017

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions