Home > Database > Mysql Tutorial > body text

在SQL中获取一个长字符串中某个字符串出现次数的实现方法_MySQL

WBOY
Release: 2016-06-01 13:25:16
Original
818 people have browsed it

bitsCN.com

在SQL中获取一个长字符串中某个字符串出现次数的实现方法

比如有个字符串:

X-BGS-2010-09-15-001

我想知道其中'-'出现的次数,可以用下面的方法实现,而不需要复杂的一个个字符分析。

declare @a varchar(100)
set @a='X-BGS-2010-09-15-001'
select len(replace(@a,'-','--'))-len(@a)

通俗一点讲就是 如果要判断表a中的 字段b中存在几个字母c的话,可以这么写

select len(replace(b,'c','--'))-len(b) from a,当然这里面的函数len可能因为不同数据库而不同

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template