if exists (select 1
from sysobjects
where id = object_id('v_RandomNumber_')
and type = 'V')
drop view v_RandomNumber_
go
CREATE VIEW v_RandomNumber_
AS
SELECT RAND() as RandomNumber_
GO
if exists (select 1
from sysobjects
where id = object_id('p_ScrambleText_')
and type in ('IF', 'FN', 'TF'))
drop function p_ScrambleText_
go
CREATE FUNCTION p_ScrambleText_(@Value_ varchar(255))
RETURNS varchar(255)
AS
BEGIN
DECLARE @Length_ int
DECLARE @RetVal_ varchar(255)
DECLARE @Counter_ smallint
DECLARE @RandomNumber_ float
DECLARE @CurChar_ as char(1)
DECLARE @ASCIIValue_ as smallint
SET @Length_ = LEN(@Value_)
SET @RetVal_ = ''
SET @ASCIIValue_ = 0
SET @Counter_ = 1
WHILE @Counter_ < (@Length_ + 1)
BEGIN
--Process next available character
SET @CurChar_ = SUBSTRING(@Value_, @Counter_, 1)
--Get the ASCII value of the character we are processing
SET @ASCIIValue_ = ASCII(@CurChar_)
--Generate a new random number (remeber this generates a number between 0 and 1)
SET @RandomNumber_ = (select RandomNumber_ from v_RandomNumber_)
--Using the ASCII value see if we are working with a number
IF @ASCIIValue_ between 48 and 57
BEGIN
--Use the random number to get a value between 0 and 9 to be used as the random replacement number
SET @CurChar_ = Convert(Char,Convert(tinyint, 9 * @RandomNumber_))
END
--if not a number check for an uppercase letter
ELSE IF @ASCIIValue_ between 65 and 90
BEGIN
--Now use the random number to get a value between 1 and 26 which we will use to generate a random replacement letter of the same case
SET @CurChar_ = CHAR(Convert(tinyint, (25 * @RandomNumber_ + 1)) + 63)
END
--If not an uppercase letter check for a lowercase letter
ELSE IF @ASCIIValue_ between 97 and 122
BEGIN
--Now use the random number to get a value between 1 and 26 which we will use to generate a random replacement letter of the same case
SET @CurChar_ = CHAR(Convert(tinyint, (25 * @RandomNumber_ + 1)) + 96)
END
--NOTE: If the character being processed is not a number value or a letter (A-Z or a-Z) then we keep that character
-- This helps preserve formatting found with data like Phone Number, SSN's, emails etc.
SET @RetVal_ = @RetVal_ + @CurChar_
SET @Counter_ = @Counter_ + 1
END
RETURN @RetVal_
END
GO
if exists (select 1
from sysobjects
where id = object_id('p_ScrambleDate_')
and type in ('IF', 'FN', 'TF'))
drop function p_ScrambleDate_
go
CREATE FUNCTION p_ScrambleDate_(@Date_ datetime)
RETURNS datetime
AS
BEGIN
DECLARE @RandomNumber_ tinyint
--Generate a random number between 1 and 25 (no special reason for 25 but it is a reasonable number of years to decrement date.
SET @RandomNumber_ = Convert(tinyint, ((24) * (SELECT RandomNumber_ FROM v_RandomNumber_) + 1))
SELECT @date_ = DateAdd(d,@RandomNumber_,@date_) --Increment date by RandomNumber_ days
SELECT @date_ = DateAdd(m,@RandomNumber_,@date_) --Increment date by RandomNumber_ months
SELECT @date_ = DateAdd(yy,(@RandomNumber_ * -1),@date_) --Decrement date by RandomNumber_ years.
RETURN @Date_
END
GO
Post the table definition and we can come up with something. Does the table have a primary key (IDENTITY)? That will make things a bit easier to scramble.