Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择字符串中出现次数的更有效方法_Sql_Sql Server_Tsql_Sql Server 2008 - Fatal编程技术网

Sql 选择字符串中出现次数的更有效方法

Sql 选择字符串中出现次数的更有效方法,sql,sql-server,tsql,sql-server-2008,Sql,Sql Server,Tsql,Sql Server 2008,大家好,我有下面的脚本,这是所有的罚款和花花公子和做的工作,但我不认为它是非常好或有效的。只是想知道是否有更好更有效的方法来做同样的事情?作业是计算数字字符串中数字的出现次数,然后选择总数。例如,对于数字“014812000”,预期结果应该是6,因为我们有4 x 0次和2 x 1次。我们只关注超过1的事件。只是想补充一点,它正在用户定义的函数中使用 DECLARE @Number nvarchar(50) SET @Number = '014812000' DECLARE @count0 in

大家好,我有下面的脚本,这是所有的罚款和花花公子和做的工作,但我不认为它是非常好或有效的。只是想知道是否有更好更有效的方法来做同样的事情?作业是计算数字字符串中数字的出现次数,然后选择总数。例如,对于数字“014812000”,预期结果应该是6,因为我们有4 x 0次和2 x 1次。我们只关注超过1的事件。只是想补充一点,它正在用户定义的函数中使用

DECLARE @Number nvarchar(50)
SET @Number = '014812000'

DECLARE @count0 int
DECLARE @count1 int
DECLARE @count2 int
DECLARE @count3 int
DECLARE @count4 int
DECLARE @count5 int
DECLARE @count6 int
DECLARE @count7 int
DECLARE @count8 int
DECLARE @count9 int

DECLARE @countTotal int
SET @countTotal = 0

SET @count0 = LEN(@Number) - LEN(REPLACE(@Number, '0', '')) 
SET @count1 = LEN(@Number) - LEN(REPLACE(@Number, '1', '')) 
SET @count2 = LEN(@Number) - LEN(REPLACE(@Number, '2', '')) 
SET @count3 = LEN(@Number) - LEN(REPLACE(@Number, '3', '')) 
SET @count4 = LEN(@Number) - LEN(REPLACE(@Number, '4', '')) 
SET @count5 = LEN(@Number) - LEN(REPLACE(@Number, '5', '')) 
SET @count6 = LEN(@Number) - LEN(REPLACE(@Number, '6', '')) 
SET @count7 = LEN(@Number) - LEN(REPLACE(@Number, '7', '')) 
SET @count8 = LEN(@Number) - LEN(REPLACE(@Number, '8', '')) 
SET @count9 = LEN(@Number) - LEN(REPLACE(@Number, '9', '')) 

IF @count0 > 1
    BEGIN
        SET @countTotal = @countTotal + @count0
    END
IF @count1 > 1
    BEGIN
        SET @countTotal = @countTotal + @count1
    END
IF @count2 > 1
    BEGIN
        SET @countTotal = @countTotal + @count2
    END
IF @count3 > 1
    BEGIN
        SET @countTotal = @countTotal + @count3
    END
IF @count4 > 1
    BEGIN
        SET @countTotal = @countTotal + @count4
    END
IF @count5 > 1
    BEGIN
        SET @countTotal = @countTotal + @count5
    END
IF @count6 > 1
    BEGIN
        SET @countTotal = @countTotal + @count6
    END
IF @count7 > 1
    BEGIN
        SET @countTotal = @countTotal + @count7
    END
IF @count8 > 1
    BEGIN
        SET @countTotal = @countTotal + @count8
    END
IF @count9 > 1
    BEGIN
        SET @countTotal = @countTotal + @count9
    END

SELECT @countTotal
看一看。它使用自定义函数

FUNCTION dbo.com_CountString(@Input nVarChar(max), @SearchString nVarChar(1000))
RETURNS INT
BEGIN
DECLARE @Count INT, @Index INT, @InputLength INT, @SearchLength INT
DECLARE @SampleString INT

if @Input is null or @SearchString is null
    return 0

SET @Count = 0
SET @Index = 1
SET @InputLength  = LEN(@Input)
SET @SearchLength = LEN(@SearchString)

if @InputLength = 0 or @SearchLength = 0 or @SearchLength > @InputLength
    return 0

WHILE @Index <= @InputLength - @SearchLength + 1
BEGIN
    IF SUBSTRING(@Input, @Index, @SearchLength) = @SearchString
    BEGIN
        SET @Count = @Count + 1
        SET @Index = @Index + @SearchLength
    END
    ELSE
        SET @Index = @Index + 1
END

RETURN @Count
END
看一看。它使用自定义函数

FUNCTION dbo.com_CountString(@Input nVarChar(max), @SearchString nVarChar(1000))
RETURNS INT
BEGIN
DECLARE @Count INT, @Index INT, @InputLength INT, @SearchLength INT
DECLARE @SampleString INT

if @Input is null or @SearchString is null
    return 0

SET @Count = 0
SET @Index = 1
SET @InputLength  = LEN(@Input)
SET @SearchLength = LEN(@SearchString)

if @InputLength = 0 or @SearchLength = 0 or @SearchLength > @InputLength
    return 0

WHILE @Index <= @InputLength - @SearchLength + 1
BEGIN
    IF SUBSTRING(@Input, @Index, @SearchLength) = @SearchString
    BEGIN
        SET @Count = @Count + 1
        SET @Index = @Index + @SearchLength
    END
    ELSE
        SET @Index = @Index + 1
END

RETURN @Count
END

可以使用数字表将数字拆分为行。这里我使用master..spt\u值

select sum(C)
from (
       select count(*) as C
       from master..spt_values as N
       where N.type = 'P' and
             N.number between 1 and len(@Number)
       group by substring(@Number, N.Number, 1)
       having count(*) > 1
     ) as T      

可以使用数字表将数字拆分为行。这里我使用master..spt\u值

select sum(C)
from (
       select count(*) as C
       from master..spt_values as N
       where N.type = 'P' and
             N.number between 1 and len(@Number)
       group by substring(@Number, N.Number, 1)
       having count(*) > 1
     ) as T      

这将能够处理您的问题,即使数字不是数字,它也可以计算存在多个相同字符的任何出现的字符

它是为一个表格字符制作的

declare @t table(number nvarchar(max))
insert @t values ('014812000')
insert @t values ('0148120001')
insert @t values ('0148120001aa')

;with a as
(
select number n, 0 i from @t
union all
select replace(n, cast(n as char(1)), ''), 
case when replace(n, cast(n as char(1)), '') = stuff(n,1,1,'') then i else 
i + cast(len(n) - len(replace(n, cast(n as char(1)), '')) as int)
end
from a
where n <> ''
)
select i from a where n = ''
如果您只想要一个特定的数字,那么您可以这样使用它:

declare @Number nvarchar(max) 
set @Number = '014812000'  

;with a as
(
select @number n, 0 i
union all
select replace(n, cast(n as char(1)), ''), 
case when replace(n, cast(n as char(1)), '') = stuff(n,1,1,'') then i else 
i + cast(len(n) - len(replace(n, cast(n as char(1)), '')) as int)
end
from a
where n <> ''
)
select i from a where n = ''

这将能够处理您的问题,即使数字不是数字,它也可以计算存在多个相同字符的任何出现的字符

它是为一个表格字符制作的

declare @t table(number nvarchar(max))
insert @t values ('014812000')
insert @t values ('0148120001')
insert @t values ('0148120001aa')

;with a as
(
select number n, 0 i from @t
union all
select replace(n, cast(n as char(1)), ''), 
case when replace(n, cast(n as char(1)), '') = stuff(n,1,1,'') then i else 
i + cast(len(n) - len(replace(n, cast(n as char(1)), '')) as int)
end
from a
where n <> ''
)
select i from a where n = ''
如果您只想要一个特定的数字,那么您可以这样使用它:

declare @Number nvarchar(max) 
set @Number = '014812000'  

;with a as
(
select @number n, 0 i
union all
select replace(n, cast(n as char(1)), ''), 
case when replace(n, cast(n as char(1)), '') = stuff(n,1,1,'') then i else 
i + cast(len(n) - len(replace(n, cast(n as char(1)), '')) as int)
end
from a
where n <> ''
)
select i from a where n = ''

工作到底是什么?通过查看代码,至少我不太清楚。工作是计算数字字符串中出现数字的次数,然后选择总数。仅添加一点,这是在用户定义函数中使用的。因此,例如,对于数字“014812000”,预期结果应为6,因为我们有4 x 0次和2 x 1次。我们只关心超过1次的事件。@Vince Ashby Smith:我同意Mikael的观点,你的问题目前的形式还不清楚。请用您刚才在评论中所说的内容更新您的问题,因为这是一个重要的澄清。@Vince-您总是只关注数字数据还是这是针对任意字符的?具体的工作是什么?通过查看代码,至少我不太清楚。工作是计算数字字符串中出现数字的次数,然后选择总数。仅添加一点,这是在用户定义函数中使用的。因此,例如,对于数字“014812000”,预期结果应为6,因为我们有4 x 0次和2 x 1次。我们只关心超过1次的事件。@Vince Ashby Smith:我同意Mikael的观点,你的问题目前的形式还不清楚。请用您刚才在评论中所说的内容更新您的问题,因为这是一个重要的澄清。@Vince-您总是只关注数字数据,还是这是针对任意字符的?