1

I have this SQL query:

  SELECT  childUsers.userName, 
  (IF(childUsers.modified  >= Convert(datetime, '2018-11-15')
  BEGIN
  CASE 
          WHEN childUsers.ProgressID = 1 THEN 'New Level One'
           WHEN childUsers.ProgressID = 2 THEN 'New Level One Completed'
           WHEN childUsers.ProgressID = 3 THEN 'New Level Two'
           WHEN childUsers.ProgressID = 4 THEN 'New Level Two Completed'
           ELSE 'Has Not Started'
  END
  ELSE
  BEGIN
CASE 
          WHEN childUsers.ProgressID = 1 THEN 'Introduction'
           WHEN childUsers.ProgressID = 2 THEN 'Introduction Completed'
           WHEN childUsers.ProgressID = 3 THEN 'Unit 1'
           WHEN childUsers.ProgressID = 4 THEN 'Unit 1 Completed'
           ELSE 'Has Not Started'
  END) as furthestSectionVisited
FROM childUsers
WHERE childUsers.usergroupId=112888 

I want to set a conditional IF statement which will check a specific field in the table, if the modified table is older from a specific date then the values returned will need to be different.

In this case I'm getting error, am I missing something or something needs to be modified?

2
  • IF is used to control of statements. You need to modify your case expression to include both date and progressID logic. Commented Jan 28, 2019 at 19:35
  • Hi Laziale. A quick reminder that the community prefers questions without chit-chat, greetings and signatures. We don't have enough editors as it is here, so if you could refrain from adding these things, it really does help us out! Commented Jan 29, 2019 at 9:12

1 Answer 1

3

You will need to use nested case expressions here. IF is not a way to add dynamic segments to a query.

SELECT  childUsers.userName, 
    case when childUsers.modified  >= Convert(datetime, '2018-11-15') then
        case childUsers.ProgressID
            when 1 THEN 'New Level One'
            WHEN THEN 'New Level One Completed'
            WHEN THEN 'New Level Two'
            WHEN THEN 'New Level Two Completed'
            ELSE 'Has Not Started'
        END
    ELSE
        CASE childUsers.ProgressID
            WHEN 1 THEN 'Introduction'
            WHEN 2 THEN 'Introduction Completed'
            WHEN 3 THEN 'Unit 1'
            WHEN 4 THEN 'Unit 1 Completed'
            ELSE 'Has Not Started'
        END
    END as furthestSectionVisited
FROM childUsers
WHERE childUsers.usergroupId=112888 
Sign up to request clarification or add additional context in comments.

2 Comments

Too fast for me :-)
@ZoharPeled most of it was written already. Just copy and paste with a move adjustments. :D

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.