MySQL search for “$” (dollar sign) fails?

给你一囗甜甜゛ 提交于 2019-12-14 03:52:10

问题


Been asked to find dollar values across a large body of text. Can a search be performed on the dollar sign? If so, any examples/guidance would be most appreciated. Current query...

select * from concept where concept_description like '%$%';

回答1:


The queries given will select the rows where concept_description contains a $, but I assume that you want to actually pull out the dollar amounts? If there's only ever just one dollar amount in a field it can be pulled out using

SELECT
SUBSTRING(
    concept_description,
    LOCATE('$', concept_description),
    LOCATE(' ', concept_description, LOCATE('$', concept_description)) - LOCATE('$', concept_description)
)
FROM table
WHERE LOCATE('$', concept_description) > 0

This assume that the dollar amount is always followed by a space, and might need some fudging on the indexes. It's probably best to pull the full field out with a simple query, then use regular expressions to grab any dollar values.




回答2:


You may want to use LOCATE to see if the col value contains the $ e.g.

   SELECT * FROM concept WHERE LOCATE('$', concept_description) > 0;


来源:https://stackoverflow.com/questions/13035906/mysql-search-for-dollar-sign-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!