-1

I am trying to find the date of 7 days from a specific date. To do this I tried

Select @Fromdate = DATEADD(day,-7, max(last_update)), @todate= max(last_update)
FROM vwABC

But it is not working and when I tried the following every thing works fine.

SELECT @Fromdate = '20150601', @ToDate = MAX(last_update)
FROM vwABC

I cant understand, whats the problem. Please help.

jarlh
  • 42,561
  • 8
  • 45
  • 63
Dr. Mian
  • 3,334
  • 10
  • 45
  • 69
  • 2
    "But it is not working", can you be more specific?, what doesn't work? – Lamak Jun 04 '15 at 14:30
  • 2
    And also, tell us what the value of `last_update` is, and what you are expecting to get back. – sstan Jun 04 '15 at 14:31
  • "7 days from now" would more likely be `DATEADD(day,7` than `DATEADD(day,-7` but without more context, it's difficult to know what you're trying to do. – Damien_The_Unbeliever Jun 04 '15 at 14:37
  • What behavior are you expecting, and what behavior do you get? – TZHX Jun 04 '15 at 14:37
  • Try this Query first and check are you getting your desired Date or not Select DATEADD(day,-7, max(last_update)) FROM vwABC – Chiragkumar Thakar Jun 04 '15 at 14:41
  • "7 days from now" I meant 7 days ago so I think -7 is fine. On the basis of it i am calculating an activity which is counted as 0 when i use dateadd but if i use plan 20150601, it gets the value. – Dr. Mian Jun 04 '15 at 14:41
  • I am getting the desired date. – Dr. Mian Jun 04 '15 at 14:42
  • @Asbat then nothing is wrong in the code you posted here, you might doing mistake somewhere else – Chiragkumar Thakar Jun 04 '15 at 14:45
  • That is very strange, so I found that if the time portion is included then the calculations are 0. When I tried ATfromdate = 2015-05-23 00:00:00.000 I got results 0. when I tried ATfromdate 2015-05-23 I got the correct results. I have some datediff methods down there to calculate results, which may get confused. Any suggestions how to truncate the time part from the dateadd before assigning to ATfromdate – Dr. Mian Jun 04 '15 at 14:48

1 Answers1

0

This is what worked for me through this reference stackoverflow question

Select @Fromdate = convert(date, DATEADD(day,-7, max(last_update)),101),  @todate= max(last_update) FROM vwABC
Community
  • 1
  • 1
Dr. Mian
  • 3,334
  • 10
  • 45
  • 69