1

I'm hoping this is a simple question, but I can't figure out how to write my query to get the correct result!

I'm trying to translate a query from a report on our ERP solution (Dynamics NAV) into T-SQL.

I've gotten stuck with a condition of the report. In NAV, it retrieves all of the data using a set of filters - which is fine. After it retrieves the data, it goes through record by record, and 'skips' records which meet 2 conditions.

The condition for records being skipped are: If "Field A" <> 4 AND "Field B" = 0 THEN SKIP.

The closest i've got to recreating this in T-SQL is:

Select *
From t
WHERE [...] AND ( [FieldA] <> 4 AND [FieldB] = 0 ) 

This does not work!

How can I exclude records in T-SQL, only where ((A <> 4) AND (B = 0)) ?

1 Answer 1

2

The logic equivalence not(A and B) === not(A) or not(B) is appropriate here.

In this case A means "Field A" <> 4 and B means "Field B" = 0. So not(A) means "Field A" = 4 and not(B) means "Field B" <> 0.

Hence

SELECT *
FROM t
WHERE [...]
    AND
    (
        [FieldA] = 4 OR
            [FieldB] <> 0
    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.