CREATE function [dbo].[GetCharIndexNum](@findstring varchar(max),@string varchar(max))
returns int
AS
BEGIN
declare @location int , --要找的字符位置
@num int --要找的字符出现的次数
set @num =0
set @location = charindex (@findstring,@string)
while @location >0 ---字符串中存在要找的字符
begin
set @num =@num +1
set @string =substring(@string,@location+1,len(@string))
set @location = charindex (@findstring,@string)
end
return @num
END
--举个例子调用这个标量值函数 select [dbo].[GetCharIndexNum]('5','abc5ab5')
返回值2,5这个字符出现了2次
select * from post order by length(content) desc limit 1这样每次取速度太慢,可以通过before insert的触发器统计数字,然后通过数字索引查询。
// assume field lenofcontent exists in table postselect * from post order by lenofcontent desc limit 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)