How do I sort string alphabetically

前端 未结 4 1812
半阙折子戏
半阙折子戏 2020-12-07 02:33

I am new to SQL Server as well as to Stack overflow. Please excuse for my mistakes.

Is it possible to sort a value in a column aphabetically? Here is my table

<
4条回答
  •  离开以前
    2020-12-07 03:02

    Even some optimized possibilities are there. Here two function use Bubble sort to sort the char.

    CREATE FUNCTION udf_SortString
    (
        @string VARCHAR(1000)
    )
    RETURNS VARCHAR(1000)
    AS
    BEGIN
        DECLARE @len TINYINT
        DECLARE @i TINYINT
        DECLARE @currentchar CHAR(1)
        DECLARE @swapped BIT
        DECLARE @begin BIT
        DECLARE @nextchar CHAR(1)
    
        SET @begin = 1
        SET @len = LEN(@string)
        SET @i = 1
    
        WHILE @begin = 1 OR @swapped = 1
        BEGIN
            SET @swapped = 0
            SET @i = 1
            SET @begin = 0
            WHILE @i <= @len
            BEGIN
                SET @currentchar = SUBSTRING(@string, @i, 1)
                SET @nextchar = SUBSTRING(@string, @i + 1, 1)
    
                IF @currentchar > @nextchar AND (@nextchar > '')
                BEGIN
                    SET @string = dbo.udf_swap(@string, @i, @i + 1)
                    SET @swapped = 1
                END
    
                SET @i = @i + 1
            END
        END
    
        RETURN(@string)
    END
    

    Function 2:

    CREATE FUNCTION dbo.udf_Swap
    (
        @fullstring VARCHAR(1000),
        @charlocation1 TINYINT,
        @charlocation2 TINYINT
    )
    RETURNS VARCHAR(1000)
    AS
    BEGIN
            DECLARE @returnval varchar(1000)
            DECLARE @begin VARCHAR(1000), @middle VARCHAR(1000), @end VARCHAR(1000)
            DECLARE @firstchar CHAR(1), @secondchar CHAR(1), @len INT
            SET @fullstring = LTRIM(RTRIM(@fullstring))
            SET @len = LEN(@fullstring)
    
        IF @charlocation1 > @len OR @charlocation2 > @len
            SET @returnval = @fullstring
            ELSE
            BEGIN
                   SET @firstchar = SUBSTRING(@fullstring, @charlocation1, 1)
                   SET @secondchar = SUBSTRING(@fullstring, @charlocation2, 1)
                   SET @begin = LEFT(@fullstring, (@charlocation1-1))
                   SET @middle = SUBSTRING(@fullstring, @charlocation1+1, (@charlocation2-@charlocation1)-1)
                   SET @end = SUBSTRING(@fullstring, @charlocation2+1, @len)
                   SET @returnval = @begin + @secondchar + @middle + @firstchar + @end
            END
        RETURN(@returnval)
    END
    

    Result:

    select dbo.udf_SortString('zxcvbfgrtyuijklm')
    --Returns bcfgijklmrtuvxyz
    

    --Reference

    --Quick demo here

提交回复
热议问题