存储架构

SQL selects the first character after each occurrence of a specific string character

微信扫一扫,分享到朋友圈

SQL selects the first character after each occurrence of a specific string character
0

I am trying to create a string containing the first character of a string and the first character following each occurrence of a specific character in the string. For example if ‘.’ is the specific character, ‘Hello.Bye.Today.Yesterday’ should return ‘HBTY’.

What I have so far:

SELECT substring(Hello.Bye.Today.Yesterday, 1,1)

If you want to use SQL for this, create a function with the following logic

DECLARE @value VARCHAR(255)
SET @value = 'Hello.Bye.Today.Yesterday'

DECLARE @result VARCHAR(255)
DECLARE @curChar CHAR(1)

DECLARE @start INT, @end INT
SET @start = 2
SET @end = LEN(@value)

SET @result = SUBSTRING(@value, 1,1)

WHILE @start < @end
BEGIN
    SET @curChar = SUBSTRING(@value, @start, 1)
    IF @curChar = '.'
    BEGIN
        SET @result = @result + SUBSTRING(@value, @start + 1, 1)
        SET @start = @start + 1
    END
    SET @start = @start + 1
END

Result

HBTY

阅读原文...


微信扫一扫,分享到朋友圈

SQL selects the first character after each occurrence of a specific string character
0

Hello, buddy!

Recycle your HTML code

上一篇

Amazon is buying mesh router company Eero

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

SQL selects the first character after each occurrence of a specific string character

长按储存图像,分享给朋友