简体   繁体   中英

NULL when value not found using substring_index in MYSQL

I am trying to pass out 2 values from a large field within my MYSQL database, to achieve this I am using the substring_index function.

The function works exactly as it should until it comes to a field where the values that I am looking for within the function don't exist. When this happens I just get some random part of the entire field. What I want to happen is if either search value of the function cannot be found, just return NULL.

The code I am using is as follows:

select id,
substring_index(substring_index(field, 'QF=',-1), 'RF=',1) as gscore
from 
tablename

So in short if "QF=" or "RF=" are not within the field then return NULL.

Thanks

You can use an IF function to acheive that.

select id,
   IF(substring_index(substring_index(field, 'QF=',-1), 'RF=',1)=field,NULL,
   substring_index(substring_index(field, 'QF=',-1), 'RF=',1)) as gscore
from 
tablename

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