Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:53:27+00:00 2026-06-15T19:53:27+00:00

Possible Duplicate: CASE equivalent of a nested IIF statement I’m trying to convert the

  • 0

Possible Duplicate:
CASE equivalent of a nested IIF statement

I’m trying to convert the following Access query into T-SQL, but I’m having problems with IIF statement.

ACCESS Query

SELECT 
qry_LAB_LOAD_Prequery2.Asset AS SITE_REF,
Format([Det],"00000") AS DET_, 
IIf(IsNull([MT]),"0",IIf(Right([SP_Ref],2)="WZ"
And IsNull([LabLoadFileSuffix_MT]) 
And [MT]>0,CStr([MT]) & "CT",CStr([MT]) & [LabLoadFileSuffix_MT])) AS MT_COUNT,  
IIf(IIf(IsNull([MT]),0,[MT])>=IIf(IsNull([IM]),0,[IM]),0,
IIf(IsNull([IM]),"0",IIf(Right([SP_Ref],2)="WZ" 
And IsNull([LabLoadFileSuffix_IM]),CStr([IM]) 
& "CT",CStr([IM]) & [LabLoadFileSuffix_IM]))) AS IM_COUNT,   
qry_LAB_LOAD_Prequery2.[2012 Sample Point] AS SP_REF, qry_LAB_LOAD_Prequery2.Area
FROM qry_LAB_LOAD_Prequery2
WHERE (((IIf(IsNull([IM]),0,[IM])+IIf(IsNull([MT]),0,[MT]))>0));

I’ve tried to convert a part of it. Can anyone help me correct the nested IID statement as I’m not sure.

SQL Query

SELECT qry_LAB_LOAD_Prequery2.Asset AS SITE_REF,
RIGHT('00000' + CAST([Det] AS VARCHAR(5)),5) AS DET_,
  ----- (the nested iff statements)
qry_LAB_LOAD_Prequery2.[2012 Sample Point] AS SP_REF,
qry_LAB_LOAD_Prequery2.Area
FROM qry_LAB_LOAD_Prequery2
where (ISNull([IM],0) + ISNULL([MT],0)) > 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:53:28+00:00Added an answer on June 15, 2026 at 7:53 pm

    To convert the nested IIF you need user CASE.

    Lets take the first one:

    Reformating I get:

    IIf(
              IsNull([MT]),
              "0",
                       IIf(Right([SP_Ref],2)="WZ"
                                And IsNull([LabLoadFileSuffix_MT]) 
                                And [MT]>0,
    
                            CStr([MT]) & "CT",
                            CStr([MT]) & [LabLoadFileSuffix_MT]
                        )
        ) AS MT_COUNT, 
    

    It Converts to :

    CASE
    WHEN [MT] IS NULL 
        THEN "0",
    ELSE
        CASE 
            WHEN Right([SP_Ref],2) = "WZ"
               AND ([LabLoadFileSuffix_MT] IS NULL
                   AND [MT]>0
            THEN 
                 CAST([MT] AS VARCHAR) + "CT"
            ELSE
                 CAST([MT] AS VARCHAR) + [LabLoadFileSuffix_MT]
        END
    END AS MT_COUNT,  
    

    Which simplifies to :

    CASE
    WHEN [MT] IS NULL 
        THEN "0",
    ELSE
        CAST([MT] AS VARCHAR)  
       + CASE 
            WHEN Right([SP_Ref],2) = "WZ"
               AND ([LabLoadFileSuffix_MT] IS NULL
                   AND [MT]>0
            THEN 
                 "CT"
            ELSE
                 [LabLoadFileSuffix_MT]
        END
    END AS MT_COUNT, 
    

    For tee second one I reformatted and then converted it to the following

    CASE WHEN 
    
        CASE 
          WHEN [MT] IS NULL THEN 0 
          ELSE [MT] 
        END 
    
         >=
    
        CASE 
          WHEN [IM] IS NULL THEN 0 
          ELSE [IM] 
        END ,
    
     THEN   0,
     ELSE    
    
       CASE 
          WHEN [IM] IS NULL THEN "0" 
          ELSE 
    
            CASE 
              WHEN Right([SP_Ref],2) = "WZ" AND [LabLoadFileSuffix_IM] IS NULL THEN CAST([IM] AS VARCHAR) + "CT"
              ELSE CAST([IM] AS VARCHAR) + [LabLoadFileSuffix_IM]
            END
          END
     END
     AS IM_COUNT,
    

    Whick reduces to

    CASE 
          WHEN ISNULL([MT], 0) > =  ISNULL([IM], 0)
     THEN   0,
     ELSE    
       CASE 
          WHEN [IM] IS NULL THEN "0" 
          ELSE 
            CAST([IM] AS VARCHAR) +
            CASE 
              WHEN Right([SP_Ref],2) = "WZ" AND [LabLoadFileSuffix_IM] IS NULL THEN "CT"
              ELSE  [LabLoadFileSuffix_IM]
            END
          END
     END
     AS IM_COUNT,
    

    Which essentially boils down to 2 very similar expressions for both columns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: oracle PL/SQL: sort rows I run this query: Select a.product, sum( case
Possible Duplicate: SQL server ignore case in a where expression basically I need to
Possible Duplicate: mysql case sensitive query 'm working on an PHP script that checks
Possible Duplicate: Is there a good reason to use upper case for T-SQL keywords?
Possible Duplicate: Why copy constructor is not called in this case? Considering the following
Possible Duplicate: Why copy constructor is not called in this case? In the following
Possible Duplicate: WHERE Something IN (CASE WHEN statement) ??!! i want ro write a
Possible Duplicate: Convert a String In C++ To Upper Case Hi, I need a
Possible Duplicate: Oracle DB: How can I write query ignoring case? I have an
Possible Duplicate: ‘break’ statement when using curly braces in switch-case While merging a package

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.