Results 1 to 14 of 14
  1. #1
    soldat452002 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    16

    SQL Question , Convert Formula

    Hello

    Im trying to create this Excel formula in SQL. How can I do this in SQL.


    Data Sources

    Contract.StatusCode



    License.LicenseExpiration

    Education.ProductTraining

    Contract.StatusEffective


    =IF([@[STATUS CODE]]<>"COMPLETE", "NOT ELIGIBLE TO SELL",IF([@[LICENSE EXPIRATION]]<=TODAY(),"NOT ELIGIBLE TO SELL",IF([STATUS EFFECTIVE]>=[PRODUCT TRAINING],[STATUS EFFECTIVE],[PRODUCT TRAINING])))

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Look at the IIf() function. The Date() function returns the current date, the Now() function returns the current date and time.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    As paul says - a fairly easy translation from excel to access. Although not applicable in the example provided be aware with regards the access iif function that you have to provide a resolution for both true and false. In the case of excel, the IF function will work with only a true resolution e.g.

    IF(a=b,true,false) works
    IF(a=b,true) works

    iif(a=b,true,false) works
    iif(a=b,true) fails

  4. #4
    soldat452002 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    16
    Quote Originally Posted by Ajax View Post
    As paul says - a fairly easy translation from excel to access. Although not applicable in the example provided be aware with regards the access iif function that you have to provide a resolution for both true and false. In the case of excel, the IF function will work with only a true resolution e.g.

    IF(a=b,true,false) works
    IF(a=b,true) works

    iif(a=b,true,false) works
    iif(a=b,true) fails

    It does not work in SQL.


    SELECT DISTINCT

    BROKERID,
    CONTRACTID,
    JURISDICTIONCD AS "LICENSE STATE",
    TERMINATIONREASONCODE AS "STATUS CODE",

    IF(TERMINATIONREASONCODE = "COMPLETE", TRUE, FALSE) AS "WORKS"


    FROM Producer P

    INNER JOIN ProducerVer PV on PV.ProducerNo = P.ProducerNo
    INNER JOIN Contract C on C.ProducerNo = P.ProducerNo
    INNER JOIN ContractVer CV on CV.ContractNo = C.ContractNo



    WHERE CV.PROSTA = 1 AND CONTRACTTYPE IN ('Selling Agent', 'Producer')

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    you use iif, not if

  6. #6
    soldat452002 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    16
    Quote Originally Posted by Ajax View Post
    you use iif, not if

    It is a SQL oracle database. not access. sorry

    Only thing Im missing is Returning the highest date in the bold section. I was able to do that in Access.

    SELECT P.PRODUCERID, C.CONTRACTID, TERMINATIONREASONCODE, to_char(to_date(DateTERM, 'yyyy/mm/dd'), 'mm/dd/yyyy') AS "LIC Expiration", to_char(to_date(E.COMPLETIONDATE, 'yyyy/mm/dd'), 'mm/dd/yyyy') AS "trAINING DATE",
    to_char(to_date(CV.DATEEFF, 'yyyy/mm/dd'), 'mm/dd/yyyy') AS "STATUS EFF",
    CASE
    WHEN CV.TERMINATIONREASONCODE <> 'COMPLETE' THEN 'NOT ELIGIBLE TO SELL'
    WHEN LV.DATETERM < '20180726' THEN 'NOT ELIGIBLE TO SELL'
    WHEN CV.DATEEFF > E.COMPLETIONDATE THEN 'RETURN HIGHEST DATE OF TWO'
    ELSE 'ELIGIBLE TO SELL'
    END AS "ETS"

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    @Ajax, for IIf() in Access (ControlSource and VBA expressions) calcs must have valid result but IIf() in query does not.

    IIf(1=1, 10/2, 10/0)

    Will fail in Access but works fine in query.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    IIf() in query does not.
    that wasn't my point - IIf(1=1, 10/2) in a query will fail as an incomplete expression

    @Soldat. My Oracle sql is rusty but wouldn't

    WHEN CV.DATEEFF > E.COMPLETIONDATE THEN 'RETURN HIGHEST DATE OF TWO'

    be simply

    WHEN CV.DATEEFF > E.COMPLETIONDATE THEN CV.DATEEFF


  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Well, you'd need an Else to return the other value, which means a nested Case statement for the dates.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Hmmm bit like the answer here ?
    https://www.accessforums.net/showthread.php?t=73072

    Duplicate question I think??
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Yup. You need the Case in yours I think.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Agreed - edited suitably !
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    IIf(1=1, 10/2) tested in query and no error.
    Same for IIf(1=2, 10/2)

    Both will fail in Access/VBA.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    OK, OK I stand corrected

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Formula Question?
    By spyldbrat in forum Access
    Replies: 3
    Last Post: 12-24-2015, 10:08 AM
  2. Replies: 8
    Last Post: 11-25-2015, 11:38 AM
  3. Replies: 3
    Last Post: 07-10-2015, 01:19 PM
  4. Convert Formula to String without Evaluating
    By tkilback in forum Modules
    Replies: 5
    Last Post: 05-15-2015, 03:18 PM
  5. Replies: 5
    Last Post: 12-14-2011, 08:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums