Data scrubbing using SQL functions for SQL Server

Many a times, it is required to scrub the data before it is used in development environments as the data may be sensitive and cannot be shared with the vendors. The  below SQL functions can be used to scrub the data.


--Creating the User defined function 
  CREATE FUNCTION getRandomText(@sLength TINYINT)
  RETURNS VARCHAR(10)
  AS
                BEGIN
                 DECLARE @counter TINYINT
                 DECLARE @nextChar CHAR(1)
                 DECLARE @randomString VARCHAR(50)
 
                 SET @sLength=10
                 SET @counter = 1
                 SET @randomString = ''

                 WHILE @counter <= @sLength
                 BEGIN
                 SELECT @nextChar = CHAR(ROUND((SELECT r FROM getRand) * 93 + 33, 0))
                 IF ( (ASCII(@nextChar) between 65 and 90)  or (ASCII(@nextChar) between 97 and 122) )
                   BEGIN
                   SELECT @randomString = @randomString + @nextChar
                   SET @counter = @counter + 1
                   END
                 END
                return @randomString
                END
               
    GO
   
   -- Creating the User defined function
  CREATE FUNCTION getRandomNumber()
                RETURNS INT
                AS
                                BEGIN
                                                RETURN ROUND((SELECT r FROM getRand) * 10000, 0)
                                END
                GO

Comments

Popular posts from this blog

The famous Vanilla story --

Convert fetchXML to SQL Query in MSCRM 4.0

DOGL - Day ONE GO LIVE