计算SQL字符串值中字符出现次数?

时间:2012-04-05 10:53:03

标签: sql sql-server-2008

如果您有电子邮件地址

you.late.you@asdf.com

you_late_you@asdf.com

如果使用sql代码有2个点/下划线,如何计算?

5 个答案:

答案 0 :(得分:6)

select len('you.late.you@asdf.com') - len(replace('you.late.you@asdf.com', '.', ''))

答案 1 :(得分:1)

这将为您提供所需的结果。

 DECLARE @str VARCHAR(1000)
SET @str = 'you.l_ate.you@as_df.com'
SELECT (LEN(@str)- LEN(REPLACE(@str ,'.' ,'')))+(LEN(@str)- LEN(REPLACE(@str ,'_' ,'')))

回答:5

答案 2 :(得分:1)

我想他想在@:

之前计算没有点的字母
declare @myEmail varchar(50)
set @myEmail = 'you.late.you@asdf.com'

declare @mySearch varchar(50)
set @mySearch = SUBSTRING (@myEmail,0 , PATINDEX( '%@%',@myEmail))
select (LEN(REPLACE(@mySearch, '.', '')))

答案 3 :(得分:0)

如果要计算包含两个._的行数,可以执行以下操作:

select count(*) 
  from mytable 
 where left(email, charindex('@', email)) like '%[._]%[._]%'

leftcharindex用于忽略域名(具有.)。

参考:LIKE (TSQL, SQL Server 2008)

答案 4 :(得分:0)

select 
    length(substr('you.late.you@asdf.com',1,INSTR('you.late.you@asdf.com','@',1))) - 
    length(replace(substr('you.late.you@asdf.com',1,INSTR('you.late.you@asdf.com','@',1)), '.', '')) 
from dual