用SQL sever 语句,统计一个字符串的字母、数字、空格的个数,分别用列...
发布网友
发布时间:2022-04-29 16:27
我来回答
共1个回答
热心网友
时间:2022-05-03 19:47
CREATE TABLE TSET_VAR
(
NAME VARCHAR(200)
)
INSERT INTO TSET_VAR VALUES ('AAS12');
INSERT INTO TSET_VAR VALUES ('1AA@@S12');
INSERT INTO TSET_VAR VALUES ('12##1A');
INSERT INTO TSET_VAR VALUES ('AA S 1# 2');
create function tj
(
@str varchar(8000)
)
returns @s table (空格 int,数字 int,字母 int,其他 int)
AS
BEGIN
declare @i int = 1
declare @kg int = 0
declare @sz int =0
declare @zm int =0
declare @qt int =0
declare @x varchar(2000)
while(@i<=(LEN(@str)))
begin
set @x =SUBSTRING(@str,@i,1)
IF (@x =' ')
begin
set @kg =@kg+1
end
else if (PATINDEX('[0-9]',@x)!=0)
begin
set @sz=@sz +1
END
ELSE IF (PATINDEX('[A-Za-z]',@x))!=0
begin
set @zm =@zm+1
end
else
begin
set @qt =@qt+1
end
set @i=@i+1
end
INSERT @s VALUES(@kg,@sz ,@zm,@qt )
return
END
SELECT * FROM TSET_VAR OUTER APPLY DBO.TJ(NAME)
参考:http://zhidao.baidu.com/link?url=xQnt4y1ElEV-VlhKZsuzj3AI449l-2Ca9kVB-ZyK46SrqEo698Qydb2Wsjx7mUFS7vldmXTlMJwoG-RKtOfWX1FbJyEAKmMyUm0DZ0hKEli