简体   繁体   中英

SQL how create a Boolean column based on the value of another column

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

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

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.

select column, 
       case when column like '%mb%' then 1
       else 0 end 
from table

This works for both MSSQL and MYSQL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM