Access query view in SQL?

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));

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leigh PurvisDatabase DeveloperCommented:
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;
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));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leigh PurvisDatabase DeveloperCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.