简体   繁体   中英

mysql SELECT with SUBSTRING, LOWER and REPLACE commands

Im trying to do a select on the company name(cpnm) field of our company table but only giving me the first 6 characters, replacing all spaces plus making it lowercase.

SELECT *,
SUBSTRING(LOWER(cpnm), -LENGTH(cpnm), 6) as test
FROM company
LIMIT 100

The above works fine but as soon as I try to add the replace spaces (shown below), it doesnt give back results.

SELECT *,
SUBSTRING(LOWER(REPLACE(cpnm, ' ', '')), -LENGTH(cpnm), 6) as test
FROM company
LIMIT 100

Any ideas?

I guess by replacing the spaces you change the length of the string, therefore substr() doesn't work like you expect it.

Try SELECT *, SUBSTRING(LOWER(REPLACE(cpnm, ' ', '')), -LENGTH(REPLACE(cpnm, ' ', '')), 6) as test instead!

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