更改字符但保持长度

时间:2016-02-29 01:34:16

标签: sql sql-server sql-server-2014

我正在将敏感数据迁移到数据库,我需要隐藏文本的详细信息。我们希望保留文本的数量和长度,但要改变含义。

例如:

“詹姆斯一直很受欢迎,如果他发现很难说话,应该得到帮助”

应改为:

“jhdfy dfw aslk dfe kjdfkjd,kjf kjdsf df iotryy erhr lsdj jf ytwe it kjdf tr kjsdd”

有没有办法更新所有行,将列文本设置为此随机类型文本?真的只想改变特征(a-z,A-Z),并保留其余部分。

3 个答案:

答案 0 :(得分:0)

一种选择是使用一堆嵌套替换。 。 。但这可能会触及嵌套函数的最大数量。

您可以使用void*编写一个痛苦的查询:

outer apply

但是,您可能想要编写自己的函数。在其他数据库中,这称为select from t outer apply (select replace(t.col, 'a', 'z') as col1) outer apply (select replace(col1, 'b', 'y') ) outer apply . . . (在Unix命令之后)。如果你是Google translate(),我想你会在网上找到例子。

答案 1 :(得分: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

答案 2 :(得分: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, (@UpperZ-@UpperA)))
          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, (@LowerZ-@LowerA)))
          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