Monthly Archives: October 2011

A Sql Server Scramble Text Function

Sometimes you might want to scramble some text, for example if you want a randomized string for a password or key of some sort. What real world application could such a function be used?  In cryptography, for example if you need to scramble selected letters, numbers and symbols into a stronger password. Or in some word puzzles, one might be challenged to find the real word. Whatever the need, for applications that prefer to conduct the scrambling of text from within a database one approach is to create a temporary table to hold the values of single letters in each row and create a randomly generated value as a key to sort by. When sorted on the random number, the letters are then scrambled in a hard to predict pattern.

It would be useful to be able to run this:

SELECT [dbo].[ScrambleText](‘This_String_is_100%_Organized!’)

With the result being something like this:

adires_nnh%0t!rii1__gS0_OzsgiT

The code to create such a function is listed below: