We help IT Professionals succeed at work.

SQL 2008 - Date Filter for TODAY and TODAY-30 Days

allenkent asked
Last Modified: 2017-05-25
I have this date format in my view (table) :  2017-03-10 13:45:16.567

I am trying to get today only in my view. I have tried the following:

SELECT     TOP (100) PERCENT DateCompleted
FROM         dbo.tblSuperDateChecker
WHERE     (DateCompleted = RTRIM(CONVERT(char(10), GETDATE(), 101)))

SELECT     TOP (100) PERCENT DateCompleted
FROM         dbo.tblSerialMasterPending
WHERE     (DateCompleted = CONVERT(Varchar(8), GETDATE(), 112))

I have piece of data that should be showing: (here is date filter removed). The top line should display because today is the 10th
2017-03-10 13:45:16.567
2017-03-09 13:45:13.550
2017-03-09 13:45:10.540
Watch Question

Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

CAST(GETDATE() as date) will return the date part of GETDATE() without the time.
SELECT DateCompleted
FROM dbo.tblSuperDateChecker
WHERE DateCompleted >= CAST(GETDATE() as date)

Open in new window


Perfect. Where do I put additional -30 days (today to 30 days ago)
EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
Unlock this solution and get a sample of our free trial.
(No credit card required)
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

here is another approach which allows the 30 days ago data to be presented on the same row as today
declare @today datetime
set @today = dateadd(dd, datediff(dd,0, getDate()), 0) -- this would be the normal line

SELECT top(1) t.DateCompleted, oa.P30DateCompleted
FROM tblSerialMasterPending t
    select top(1) DateCompleted P30DateCompleted
    from tblSerialMasterPending p30
    where p30.DateCompleted >= dateadd(day,-30,@today) and p30.DateCompleted < dateadd(day,-29,@today)
    order by p30.DateCompleted DESC
    ) oa
WHERE t.DateCompleted >= @today and t.DateCompleted < dateadd(day,30,@today)
ORDER BY t.DateCompleted DESC

Open in new window

|    DateCompleted    |  P30DateCompleted   |
| 10.03.2017 13:45:16 | 08.02.2017 13:14:15 |

Open in new window

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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.