Search code examples
sql-servercommandmaskmasking

How to Sql Server Command?


I want to change 2 letters and mask the remaining letters. I changed the letters but didn't mask other letter. This change command is

SELECT NAME,
       CONCAT(SUBSTRING(NAME, 1, 2), 
              SUBSTRING(NAME, 4, 1), 
              SUBSTRING(NAME, 3, 1), 
              SUBSTRING(NAME, 5, ABS(LEN(NAME) -4))) 
       AS CHANGELETTER
FROM TESTBILGILER

How can I do masking SQL SERVER 2014?


Solution

  • A little ugly, but here is another option

    Example

    Declare @YourTable table(SomeCol varchar(50))
    Insert Into @YourTable values
     ('Chirstina')
    ,('John')
    ,('Susan')
    ,('Wil')      -- May want to wrap in a case for smaller strings
    ,('Bo')       -- May want to wrap in a case for smaller strings
    
    
    Select Masked = replicate('X',((len(SomeCol)-2)/2))
                   +substring(SomeCol,((len(SomeCol)-2)/2)+1,2)
                   +replicate('X',len(SomeCol)-((len(SomeCol)-2)/2)-2)
     From @YourTable
    

    Returns

    Masked
    XXXrsXXXX
    XohX
    XusXX
    WiX
    Bo