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
3 Answers
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
2 Comments
René Hoffmann
How would one know
pos and len?Rahul Tripathi
@RenéHoffmann:- Updated the answer. However I am not sure how OP is extracting the
'MB' string.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
Nikhil Vartak
I don't see a LOCATE function in MSSQL
Rahul Tripathi
This is not MSSQL. It is MySql