Press "Enter" to skip to content

Character Generation with T-SQL

Bill Fellows shows off what you can do with character generation in T-SQL:

The mod or modulus function will return the remainder after division. Modding a value is a handy way to constrain a value between 0 and an upper threshold. In this case, if I modded any number by 26 (because there are 26 characters in the English alphabet), I’ll get 0 to 25 as my result.

Knowing that the modulus function will give me 0 to 25 and knowing that my target character range starts at 65, I could use the previous expression to print any number’s ascii value like¬†SELECT CHAR((2147483625 % 26) + 65) AS StillB;. Break that apart, we do the modulus,¬†%, which gives us the value of 1 which we then add to the starting offset (65).

He also provides a DB Fiddle for it.