/*
ZN表
ZN
A
A+B
A+B+C
A+B+D
变成
A
B
C
D
*/
create FUNCTION CFN_ZN (@inputString VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
/*
SELECT dbo.CFN_ZN('A')--A
SELECT dbo.CFN_ZN('A+B') -- B
SELECT dbo.CFN_ZN('A+B+C') -- C
*/
BEGIN
DECLARE @lastPlusPosition INT;
DECLARE @plusCount INT; -- 用于存储 "+" 号的数量
SET @plusCount = (LEN(@inputString) - LEN(REPLACE(@inputString, '+', ''))); -- "+" 号的数量
SET @lastPlusPosition = LEN(@inputString);
WHILE @lastPlusPosition > 0 AND SUBSTRING(@inputString, @lastPlusPosition, 1) <> '+'
BEGIN
SET @lastPlusPosition = @lastPlusPosition - 1;
END
IF @lastPlusPosition > 0
BEGIN
SET @inputString =
CASE
WHEN @plusCount = 1 THEN ' ' + RIGHT(@inputString, LEN(@inputString) - @lastPlusPosition)
WHEN @plusCount = 2 THEN ' ' + RIGHT(@inputString, LEN(@inputString) - @lastPlusPosition)
ELSE @inputString
END;
END
RETURN @inputString;
END;
sql写一个函数,按照次序顺序缩进,加号越多,越多缩进
于 2023-10-08 09:45:01 首次发布