• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 747
  • Last Modified:

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

0
Lapchien
Asked:
Lapchien
1 Solution
 
Leigh PurvisDatabase DeveloperCommented:
What's going wrong with it?
Error message?
Wrong results?
0
 
dqmqCommented:
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;
0
 
hnasrCommented:
Try to replace 'yyyy' with "yyyy"
0
 
MashaCPACommented:
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));
0
 
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
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now