Generating random strings with T-SQL

前端 未结 27 1853
青春惊慌失措
青春惊慌失措 2020-11-29 18:10

If you wanted to generate a pseudorandom alphanumeric string using T-SQL, how would you do it? How would you exclude characters like dollar signs, dashes, and slashes from

27条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 18:48

    I thought I'd share, or give back to the community ... It's ASCII based, and the solution is not perfect but it works quite well. Enjoy, Goran B.

    /* 
    -- predictable masking of ascii chars within a given decimal range
    -- purpose: 
    --    i needed an alternative to hashing alg. or uniqueidentifier functions
    --    because i wanted to be able to revert to original char set if possible ("if", the operative word)
    -- notes: wrap below in a scalar function if desired (i.e. recommended)
    -- by goran biljetina (2014-02-25)
    */
    
    declare 
    @length int
    ,@position int
    ,@maskedString varchar(500)
    ,@inpString varchar(500)
    ,@offsetAsciiUp1 smallint
    ,@offsetAsciiDown1 smallint
    ,@ipOffset smallint
    ,@asciiHiBound smallint
    ,@asciiLoBound smallint
    
    
    set @ipOffset=null
    set @offsetAsciiUp1=1
    set @offsetAsciiDown1=-1
    set @asciiHiBound=126 --> up to and NOT including
    set @asciiLoBound=31 --> up from and NOT including
    
    SET @inpString = '{"config":"some string value", "boolAttr": true}'
    SET @length = LEN(@inpString)
    
    SET @position = 1
    SET @maskedString = ''
    
    --> MASK:
    ---------
    WHILE (@position < @length+1) BEGIN
        SELECT @maskedString = @maskedString + 
        ISNULL(
            CASE 
            WHEN ASCII(SUBSTRING(@inpString,@position,1))>@asciiLoBound AND ASCII(SUBSTRING(@inpString,@position,1))<@asciiHiBound
             THEN
                CHAR(ASCII(SUBSTRING(@inpString,@position,1))+
                (case when @ipOffset is null then
                case when ASCII(SUBSTRING(@inpString,@position,1))%2=0 then @offsetAsciiUp1 else @offsetAsciiDown1 end
                else @ipOffset end))
            WHEN ASCII(SUBSTRING(@inpString,@position,1))<=@asciiLoBound
             THEN '('+CONVERT(varchar,ASCII(SUBSTRING(@Inpstring,@position,1))+1000)+')' --> wrap for decode
            WHEN ASCII(SUBSTRING(@inpString,@position,1))>=@asciiHiBound
             THEN '('+CONVERT(varchar,ASCII(SUBSTRING(@inpString,@position,1))+1000)+')' --> wrap for decode
            END
            ,'')
        SELECT @position = @position + 1
    END
    
    select @MaskedString
    
    
    SET @inpString = @maskedString
    SET @length = LEN(@inpString)
    
    SET @position = 1
    SET @maskedString = ''
    
    --> UNMASK (Limited to within ascii lo-hi bound):
    -------------------------------------------------
    WHILE (@position < @length+1) BEGIN
        SELECT @maskedString = @maskedString + 
        ISNULL(
            CASE 
            WHEN ASCII(SUBSTRING(@inpString,@position,1))>@asciiLoBound AND ASCII(SUBSTRING(@inpString,@position,1))<@asciiHiBound
             THEN
                CHAR(ASCII(SUBSTRING(@inpString,@position,1))+
                (case when @ipOffset is null then
                case when ASCII(SUBSTRING(@inpString,@position,1))%2=1 then @offsetAsciiDown1 else @offsetAsciiUp1 end
                else @ipOffset*(-1) end))
            ELSE ''
            END
            ,'')
        SELECT @position = @position + 1
    END
    
    select @maskedString
    

提交回复
热议问题