0

I have a records like below :

Name                Count
123456M2.txt        NULL
123456M2.txt        15
123456M.txt         NULL
  • First record shoud show me NULL, as there is a number after "M" letter in name and Count < 0
  • Second record shoud show me 1, and there is a number after "M" letter in name and Count > 0
  • Third record should show me "some number from another field" as, there is no number after "M" letter in name.

Can you guide me on how to write this query.

I am able to do CASE - WHEN query, but not able to proceed with IF ELSE condition of letter check

CASE
    WHEN CHARINDEX('M', [FILE_NAME]) > 0 THEN 'COUNT'
4
  • You appear to have answered your own question - write it with a Case...When structure. If you want further assistance, I would suggest you edit your question to include sample data Commented Oct 18, 2018 at 15:00
  • Can you write in pseudo code what you want to do? Commented Oct 18, 2018 at 15:00
  • I will have to write a check if any number exists after the letter "M" then do this. Commented Oct 18, 2018 at 15:02
  • 2
    For the first record (sic) the count is not < 0, it is NULL. Commented Oct 18, 2018 at 15:03

3 Answers 3

4

You need to use multiple conditions for each WHEN in the CASE:

(psuedocode)

WHEN {Name contains 'M'} AND {character after 'M' is a number} AND {Count is NULL or < 0} THEN NULL
WHEN {Name contains 'M'} AND {character after 'M' is a number} AND {Count is NULL or < 0} THEN 1
etc...

there is no need to try to add IF..ELSE logic anywhere in your query.

Sign up to request clarification or add additional context in comments.

2 Comments

I am not sure on how to write the piece - character after 'M' is a number
Get the CHARINDEX of 'M' and add 1. The character at that position is the character after 'M'.
2

I would phrase this as:

select . . .,
       (case when name like '%M[0-9]%' and count > 0 then 1
             when name like '%M[0-9]%' then 0
             else "some number from another field"
        end)

A case expression evaluates the conditions in order, stopping at the first one that evaluates to "true".

Your description is a bit unclear, because NULL is not < 0.

Comments

1

Try the following

DECLARE @TBL TABLE (Name VARCHAR(25), [Count] INT);

INSERT INTO @TBL VALUES
('123456M2.txt',        NULL),
('123456M2.txt',        15),
('123456M.txt',         NULL);

SELECT *,
      CASE WHEN Name LIKE '%M[0-9]%' AND ([Count] IS NULL OR [Count] < 0) THEN --Check for NULL too cause NULL is not < 0
                NULL
           WHEN Name LIKE '%M[0-9]%' AND [Count] > 0 THEN
                1
           WHEN Name LIKE '%M.%' THEN
                555 --Other value from other column
                END AS Results
FROM @TBL;

Results:

+--------------+-------+---------+
|     Name     | Count | Results |
+--------------+-------+---------+
| 123456M2.txt |       |         |
| 123456M2.txt |    15 |       1 |
| 123456M.txt  |       |     555 |
+--------------+-------+---------+

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.