0

I got 2 tables in sql 2008

Table1
Id    Name Surname City   
1000  Alex White   London
1001  John Brown   Brussels
..

Table2
Id  Surgeon  Room  aId
1   Mike J.  A104  1000
2   Jack S.  C144  1001
...

And I have a query like:

Select a.Id,b.Id,
       a.Name,a.Surname,a.City,b.Surgeon,b.Room
into #results
from Table1 a
inner join Table2 b on a.Id = b.aId

What I want to do is to anonymize the a.Id and b.Id values for privacy, by using dummy ones instead of the real ones. I added a random mathematical operations before, like:

Select aId = a.Id * 22 / 5 + 14 * 2
      ,bId = b.Id * 12 / 4 + 7 * 3
       ...

but honestly I am not really happy what I am doing here and I am looking for a more professinal way to provide this. Any advice would be appreciated.

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82

1 Answers1

0

If you don't need to be sure the anonymized IDs are unique and you don't need to find a real ID based on an anonymized ID, you could use the CheckSum() or HashBytes() function with the strings from your Table1 and Table2:

Select aId = CheckSum(a.Name + a.Surname) % 10000
      ,bId = HashBytes('SHA1', b.Surgeon) % 10000
      ,a.Name,a.Surname,a.City,b.Surgeon,b.Room
into #results
from Table1 a
inner join Table2 b on a.Id = b.aId

If you need to be sure you have a unique value for each of the Id values in your table and you also need to find a real ID based on an anonymized ID, you can construct a lookup table as follows:

CREATE TABLE Anon
    (
    ID        INTEGER NOT NULL PRIMARY KEY,
    AnonID    UNIQUEIDENTIFIER DEFAULT NewID()
    );

this can then be used in queries where the actual ID should not be returned:

Select aID = Anona.AnonID,
       bID = Anonb.AnonID,
       a.Name,a.Surname,a.City,b.Surgeon,b.Room
   into #results
   from Table1 a inner join Table2 b on a.Id = b.aId
   inner join Anon Anona on a.Id = Anona.Id
   inner join Anon Anonb on b.Id = Anonb.Id

The Anon table would need to be maintained to ensure it contains all IDs from your Table1 and Table2.

chrisuae
  • 1,092
  • 7
  • 8