Generating random strings with T-SQL

前端 未结 27 1934
青春惊慌失措
青春惊慌失措 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:59

    For one random letter, you can use:

    select substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                     (abs(checksum(newid())) % 26)+1, 1)
    

    An important difference between using newid() versus rand() is that if you return multiple rows, newid() is calculated separately for each row, while rand() is calculated once for the whole query.

提交回复
热议问题