We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Access query view in SQL?

Lapchien asked
Medium Priority
Last Modified: 2010-08-05
What's wrong with this in SQL?

UPDATE [Service agreements] SET [Service agreements].RenewingDate = IIf(DateDiff('yyyy',[Live Date],Now())>4,[Live Date]+365*5,IIf(DateDiff('yyyy',[Live Date],Now())>3,[Live Date]+365*4,IIf(DateDiff('yyyy',[Live Date],Now())>2,[Live Date]+365*3,IIf(DateDiff('yyyy',[Live Date],Now())>1,[Live Date]+365*2,[Live Date]+365))))
WHERE ((([Service agreements].RenewingDate) Is Null));

Watch Question

Leigh PurvisDatabase Developer

What's going wrong with it?
Error message?
Wrong results?

So, what are you trying to get and what do you get?  It looks like you are just trying to make the renewing date 1 year beyond the live date.  If so, why not just:

UPDATE [Service agreements]
SET [Service agreements].RenewingDate = Dateadd('yyyy',1,[Live Date])  
WHERE [Service agreements].RenewingDate is null;
Hamed NasrRetired IT Professional

Try to replace 'yyyy' with "yyyy"
dqmq, I think Lapchien is calculating a renewal date that should fall in the current year, but the live date is when service agreement first became active, maybe a few years ago - e.g., if [Live Date] was 4/3/2004, then RenewingDate will be 4/3/2006 (this year).  So in this case you cannot just add 1 year to the [Live Date], it must be 2.

I do not see anything wrong with the SQL string itself - actually, I copied and pasted it into a blank database with only [Service agreements] table created, and it works fine.  I do have a couple of issues with the DateDiff formula, though.  First, the better idea would be to use DateAdd instead of [Live Date] + 365*n because it accounts for the leap years.  For example, using [Live Date] + 365*5, you will get RenewingDate 4/2/2006 if [Live Date] was 4/3/2001, but with DateAdd you will get 4/3/2006.  Second, what if [Live Date] is in the year 2000 or before?  Then the formula will only add five years, and the RenewingDate will fall in the past years.  Have you created the formula this way because the agreement origination date cannot be earlier than in 2001?  If years earlier than 2001 do not apply in your situation, then this SQL should work:

UPDATE [Service agreements] SET [Service agreements].RenewingDate = IIf(DateDiff('yyyy',[Live Date],Now())>4,DateAdd('yyyy',5,[Live Date]),IIf(DateDiff('yyyy',[Live Date],Now())>3,DateAdd('yyyy',4,[Live Date]),IIf(DateDiff('yyyy',[Live Date],Now())>2,DateAdd('yyyy',3,[Live Date]),IIf(DateDiff('yyyy',[Live Date],Now())>1,DateAdd('yyyy',2,[Live Date]),DateAdd('yyyy',1,[Live Date])))))
WHERE ((([Service agreements].RenewingDate) Is Null));

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Leigh PurvisDatabase Developer

Or indeed perhaps just

UPDATE [Service agreements]
SET [Service agreements].RenewingDate = IIf(DateDiff('yyyy',[Live Date],Now())>4, Dateadd('yyyy',5,[Live Date]), Dateadd('yyyy',DateDiff("yyyy",[Live Date], Now), [Live Date]))
WHERE [Service agreements].RenewingDate Is Null
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*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.