2016-02-29 86 views
0

我正在將敏感數據遷移到數據庫,並且我需要隱藏文本的詳細信息。我們希望保持文本的大小和長度,但改變意義。更改字符但保留長度

例如:

「詹姆斯受到好評,並應得到幫助的時候,他老是覺得很難講」

應更改爲:

「jhdfy DFW aslk DFE kjdfkjd, kjf kjdsf df iotryy erhr lsdj jf ytwe it kjdf tr kjsdd「

是否有更新所有行的方法,將列文本設置爲此隨機類型文本?真的只想改變字符(a-z,A-Z),並保持其餘。

回答

0

一種選擇是使用一堆嵌套替換。 。 。但是這可能會影響嵌套函數的最大數量。

你可以寫使用outer apply一個痛苦的查詢:

select 
from t outer apply 
    (select replace(t.col, 'a', 'z') as col1) outer apply 
    (select replace(col1, 'b', 'y')) outer apply 
    . . . 

但是,您可能需要編寫自己的功能。在其他數據庫中,這稱爲translate()(在Unix命令之後)。如果你的谷歌SQL Server translate,我想你會在網上找到例子。

0

一種方法是split the string character by character並用隨機字符串替換每一行。然後將它們連接起來回以獲得所需的輸出

DECLARE @str VARCHAR(MAX) = 'James has been well received, and should be helped when ever he finds it hard to speak' 

;WITH Cte(orig, random) AS(
SELECT 
    SUBSTRING(t.a, v.number + 1, 1), 
    CASE 
     WHEN SUBSTRING(t.a, v.number + 1, 1) LIKE '[a-z]' 
      THEN CHAR(ABS(CHECKSUM(NEWID())) % 25 + 97) 
     ELSE SUBSTRING(t.a, v.number + 1, 1) 
    END 
FROM (SELECT @str) t(a) 
CROSS JOIN master..spt_values v 
WHERE 
    v.number < LEN(t.a) 
    AND v.type = 'P' 
) 
SELECT 
    OrignalString = @str, 
    RandomString = (
     SELECT '' + random 
     FROM Cte FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)' 
    ) 

TRY IT HERE

0

行這是可能的使用用戶定義的函數(UDF)和的示圖。 SQL Server不允許在UDF中生成隨機數,但在視圖中允許它。參考文獻:http://blog.sqlauthority.com/2012/11/20/sql-server-using-rand-in-user-defined-functions-udf/

所以這裏是解決

CREATE VIEW [dbo].[rndView] 
AS 
    SELECT RAND() rndResult 

GO 
CREATE FUNCTION [dbo].[RandFn]() 
    RETURNS float 
AS 
BEGIN 
    DECLARE @rndValue float 
    SELECT @rndValue = rndResult 
    FROM rndView 
    RETURN @rndValue 
END 

GO 

CREATE FUNCTION [dbo].[randomstring] (@stringToParse VARCHAR(MAX)) 
RETURNS 
    varchar(max) 
AS 
BEGIN 
/* 
A = 65 
Z = 90 

a = 97 
z = 112 
declare @stringToParse VARCHAR(MAX) = 'James has been well received, and should be helped when ever he finds it hard to speak' 
Select [dbo].[randomstring] (@stringToParse) 
go 
Update SpecialTable 
Set SpecialString = [dbo].[randomstring] (SpecialString) 
go 

*/ 
    declare @StringToreturn varchar(max) = '' 
    declare @charCounter int = 1 
    declare @len int = len(@stringToParse) 
    declare @thisRand int 
    declare @UpperA int = 65 
    declare @UpperZ int = 90 

    declare @LowerA int = 97 
    declare @LowerZ int = 112 
    declare @thisChar char(1) 
    declare @Random_Number float 
    declare @randomChar char(1) 
    WHILE @charCounter < @len 
    BEGIN 
     SELECT @thisChar = SUBSTRING(@stringToParse, @charCounter, 1) 
     set @randomChar = @thisChar 
     --print @randomChar 
     SELECT @Random_Number = dbo.RandFn() 
     --print @Random_Number 
     --only swap if a-z or A-Z 
     if ASCII(@thisChar) >= @UpperA and ASCII(@thisChar) <= @UpperZ begin 
     --upper case 

      set @thisRand = @UpperA + (@Random_Number * convert(float, (@[email protected]))) 
      set @randomChar = CHAR(@thisRand) 
      --print @thisRand 
     end 
     if ASCII(@thisChar) >= @LowerA and ASCII(@thisChar) <= @LowerZ begin 
     --upper case 
      set @thisRand = @LowerA + (@Random_Number * convert(float, (@[email protected]))) 
      set @randomChar = CHAR(@thisRand) 
     end 
     --print @thisRand 

     --print @randomChar 
     set @StringToreturn = @StringToreturn + @randomChar   
     SET @charCounter = @charCounter + 1 
    END 

    --Select * from @returnList 
    return @StringToreturn 
END 



GO