Results 1 to 6 of 6
  1. #1
    shin_mitsugi is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    8

    Help Please!: SQL query Calculated column CASE WHEN

    Hey guys. I'm trying to write this in 2013 Access. The Case select is throwing off an error. I just want there to be a calculated column at the end, on the right of the query result and then TITLE.MilSrchPerfrmed is null make it a 0 and else a 1. Is it possible to do this with the SQL server CASE WHEN method or does it have to be IIF? If CASE WHEN is possible could somebody please assist me in how to do it?



    That's it. Can anybody help with this? I've got 20 more to add after this one but this one should give me the blueprint for future ones. Thank you!!!

    Code:
    SELECTENT.entityID, 
    MILE.FCRef, 
    MILE.ComplaintFiled, 
    ENT.casemarker, 
    ENT.entityrole, 
    TITLE.MilSrchPerfrmed, 
    TITLE.AttySigned,
    TITLE.CompltDrafted,
    TITLE.Compltverrecd, 
    TITLE.Compltversent, 
    TITLE.ExamStarted, 
    TITLE.ExamComplete,
    TITLE.brchltrwaived, 
    TITLE.brchltrreccorr,
    TITLE.TitleRptOrdd,
    TITLE.TitleRptRecd,
    ODOC.LNARecd,
    ODOC.OrigNoteRecd,
    ODOC.ChainofTitleCorrect,
    ODOC.ChainofTitleIncorrect,
    ODOC.ChainofTitle,
    ODOC.NoteCertExec,
    
    MSP as CASE When TITLE.MilSrchPerfrmed is null then 0 else 1 end
    
    FROM ((dbo_Entities AS ENT LEFT JOIN dbo_FC_Milestone AS MILE ON ENT.EntityNum = MILE.Entitynum) LEFT JOIN dbo_FC_Title AS TITLE ON MILE.Entitynum = TITLE.Entitynum) LEFT JOIN dbo_FC_OriginalDocuments ODOC ON MILE.Entitynum = ODOC.Entitynum
     WHERE(((ENT.entityID)NotLike"%TEST%"And(ENT.entityID)NotLike"%,%"And(ENT.entityID)NotLike"%OLD%")AND((MILE.FCRef)IsNotNull)AND((MILE.ComplaintFiled)IsNull)AND((ENT.casemarker)=1)AND((ENT.entityrole)="FORECLOSURE")AND((ENT.ClosedFlag)=0And(ENT.ClosedFlag)=0)AND((ENT.dateclosed)IsNull));

  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
    CASE is valid in SQL Server, not in an Access query. You've reversed the syntax if it's in SQL Server:

    Function AS Alias
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    the SQL in Access is not exactly Transact SQL. So Access will use VBA functions to take the place of stuff. You can use an IIF. It would probably look something like
    ODOC.NoteCertExec, IIf(TITLE.MilSrchPerfrmed = Null, 0, 1) AS MSP FROM ...

    https://msdn.microsoft.com/en-us/lib.../gg264412.aspx

    There are other functions, too, that will help to manage Null. there is Nz and IsNull not sure I have tried IsNull() in a query though.

  4. #4
    shin_mitsugi is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    8
    Thank you for the response.

    I'm by no means an expert at Access of T-SQL but I suppose what I'm trying to do is run this query's calculated columns as similarly to how it would be done in real SQL server as possible. This way it's like practicing SQL to some degree while I work this query in Access. Do you have any guidance on what might be the most beneficial practice SQL practice as far as T-SQL is concerned?

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    I am not sure. I am not aware of any DDL SQL for Access. Instead, you would use VBA code and collections to build stuff. You can create a SELECT INTO statement to create a new table, though.

    Most of the operators seem to work in Access. I would keep these functions handy ...
    https://msdn.microsoft.com/en-us/lib.../ff823033.aspx

  6. #6
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    MSP as CASE When TITLE.MilSrchPerfrmed is null then 0 else 1 end
    number of different ways

    iif(TITLE.MilSrchPerfrmed is null, 0,1) AS MSP

    iif(isnull(TITLE.MilSrchPerfrmed), 0,1) AS MSP

    abs(not isnull(TITLE.MilSrchPerfrmed)) AS MSP

    not isnull(TITLE.MilSrchPerfrmed)*-1 AS MSP

    The last two - can't remember if Access T-SQL returns 1 or -1 for true. if it returns 1 then it would just be

    not isnull(TITLE.MilSrchPerfrmed) AS MSP

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

Similar Threads

  1. Calculated Column in a Table
    By asmith533 in forum Programming
    Replies: 1
    Last Post: 02-10-2015, 07:53 AM
  2. Calculated Column
    By zbaker in forum Queries
    Replies: 1
    Last Post: 01-07-2015, 03:38 PM
  3. Replies: 3
    Last Post: 09-26-2012, 01:39 PM
  4. Calculated column in a report
    By beanhead0321 in forum Reports
    Replies: 2
    Last Post: 08-16-2011, 08:48 PM
  5. creating a calculated column
    By bold01 in forum Access
    Replies: 13
    Last Post: 02-10-2011, 08:17 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