Question

I want to use like in case statement and search string from a particular colmun if any part of that string contains 'Moderate to severe aortic stenosis' THEN instead of full string I want to display 'Moderate to Severe' if any part of that string contains 'Severe aortic stenosis with' THEN instead of full string I want to display 'Severe' if that string contains 'moderate aortic stenosis present' THEN instead of full string I want to display 'Moderate'

Below is my query not working some systax error

SELECT (CASE  a.Column when  LIKE '%Moderate to severe aortic stenosis%' THEN 'Moderate to Severe'
            when  LIKE '%Severe aortic stenosis with%' THEN 'Severe'
            when  LIKE '%moderate aortic stenosis present%' THEN 'Moderate' else ' ' end)
       from MyTable a

Please help

Was it helpful?

Solution 2

There are syntax errors in your query:

SELECT 
  CASE 
     WHEN a.Column LIKE '%Moderate to severe aortic stenosis%' THEN 'Moderate to Severe'
     WHEN a.Column LIKE '%Severe aortic stenosis with%' THEN 'Severe'
     WHEN a.Column LIKE '%moderate aortic stenosis present%' THEN 'Moderate' else ' ' 
  END
FROM 
 MyTable a

Field a.Column should appear after every WHEN in comparison with LIKE.

OTHER TIPS

Without seeing the error message it's hard to tell what the problem is.

But I suspect you want to write this like this:

SELECT CASE when a.Column LIKE '%Moderate to severe aortic stenosis%' THEN 'Moderate to Severe'
            when a.Column LIKE '%Severe aortic stenosis with%' THEN 'Severe'
            when a.Column LIKE '%moderate aortic stenosis present%' THEN 'Moderate' 
            else ' ' 
       end
       from MyTable a

I've moved the a.Column into the when portion of the case statement. I think you can only move it outside when doing an equality comparison rather than a fuzzy match.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top