3

I have an sql query that retrieves a column which values are strings. I want to create a column next to it that takes a value of 1 if the substring 'MB' is contained in the value or 0 otherwise

1
  • 2
    Could you provide your code.. Commented Sep 23, 2015 at 9:27

3 Answers 3

3

You can try it using the case when then like this:

select case when INSTR('mycol', 'MB') > 0 then 1 
       else 0 
       end as myBoolCol
Sign up to request clarification or add additional context in comments.

2 Comments

How would one know pos and len?
@RenéHoffmann:- Updated the answer. However I am not sure how OP is extracting the 'MB' string.
2

You can output another column with a calculated value like this:

select COLUMN, IF(LOCATE('MB', COLUMN) > 0, 1, 0) as STR_FOUND
  from TABLE

See the documentation of locate and if for further Information.

2 Comments

I don't see a LOCATE function in MSSQL
This is not MSSQL. It is MySql
1
select column, 
       case when column like '%mb%' then 1
       else 0 end 
from table

This works for both MSSQL and MYSQL

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.