复制代码 代码如下:
CalNumOfChtInStr
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Insus.NET
-- Create date: 2012-02-23
-- Description: Calculate the number of characters in the string
-- =============================================
CREATE PROCEDURE [dbo].[CalNumOfChtInStr]
(
@Value NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @dum TABLE ([Str] NVARCHAR(2))
DECLARE @I INT = LEN(@Value),@J INT = LEN(@Value)
WHILE @I > 0
BEGIN
--以下函数可参考:
IF [dbo].[IsInteger] (SUBSTRING(@Value, @I, 1)) = 0
BEGIN
RAISERROR('传入字符串包含其它字符,不完全是数字。',16,1)
RETURN
END
SET @I = @I - 1
END
WHILE @J > 0
BEGIN
INSERT INTO @dum VALUES(SUBSTRING(@Value, @J, 1))
SET @J = @J - 1
END
SELECT [Str],COUNT([Str]) AS [Num] FROM @dum GROUP BY [Str]
END
复制代码 代码如下:
EXECUTE [dbo].[CalNumOfChtInStr] '5487554127489423454'
结果:
复制代码 代码如下:
CalNumOfChtInStr
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Insus.NET
-- Create date: 2012-02-23
-- Update date: 2012-02-24
-- Description: Calculate the number of characters in the string
-- =============================================
ALTER PROCEDURE [dbo].[CalNumOfChtInStr]
(
@Value NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @dum TABLE ([Str] NVARCHAR(2))
DECLARE @I INT = LEN(@Value)
WHILE @I > 0
BEGIN
INSERT INTO @dum VALUES(SUBSTRING(@Value, @I, 1))
SET @I = @I - 1
END
--以下函数可参考:
IF EXISTS(SELECT TOP 1 1 FROM @dum WHERE [dbo].[IsInteger]([Str]) = 0)
BEGIN
RAISERROR('传入字符串包含其它字符,不完全是数字。',16,1)
RETURN
END
SELECT [Str],COUNT([Str]) AS [Num] FROM @dum GROUP BY [Str]
END
新闻热点
疑难解答
图片精选