Access query view in SQL?

Posted on 2006-04-03
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));

Question by:Lapchien
    LVL 44

    Expert Comment

    by:Leigh Purvis
    What's going wrong with it?
    Error message?
    Wrong results?
    LVL 42

    Expert Comment

    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;
    LVL 30

    Expert Comment

    Try to replace 'yyyy' with "yyyy"
    LVL 2

    Accepted Solution

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

    Expert Comment

    by:Leigh Purvis
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Excel as MS Access front end 11 77
    VBA to filter 17 66
    MS Access 2003 Query 2 26
    VBA double quote escaping question 8 27
    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now