我有一张以下信息表。
description: adjkfa34kj34kj33j4k3jk4
description: adfkjjkdf34_3434kjkjkj3
description: akjdfkjadfkjadkjfkjadfj
description: 34394394093040930949039
如何编辑下面的SQL查询来计算字符串中的位数(即[0-9])?
select description as num_count
from posts;
答案 0 :(得分:3)
一种方法是摆脱所有其他角色:
select length(regexp_replace(description, '[^0-9]', '', 'g'))
答案 1 :(得分:0)
试试这个,
CREATE FUNCTION dbo.udf_GetNumeric
(
@strAlphaNumeric VARCHAR(256)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @intAlpha INT
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
BEGIN
WHILE @intAlpha > 0
BEGIN
SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
END
END
RETURN @strAlphaNumeric
END
GO
/*For Example,*/
SELECT LEN(dbo.udf_GetNumeric('') )
SELECT LEN(dbo.udf_GetNumeric('asdf1234a1s2d3f4@@@') )
SELECT LEN(dbo.udf_GetNumeric('123ghdfhdhh456'))
SELECT LEN(dbo.udf_GetNumeric(NULL) )
SELECT LEN(dbo.udf_GetNumeric(description))AS num_count 来自帖子;
答案 2 :(得分:0)
在此之前,我使用了以下内容:
SELECT CARDINALITY(REGEXP_EXTRACT_ALL('a123', '\d')) -- returns 3