Reputation: 290
I'd like to export a table while masking a specific column, something like:
SELECT randomMask(userId), name, location FROM travel_location;
Requirements:
example result:
Upvotes: 2
Views: 4720
Reputation: 1270021
You can use checksum()
to get a reasonable random value:
select checksum(userId)
Of course, there are collisions, but that is probably acceptable.
Alas, this will be the same each time you run the query. To fix that, you could add a random number or use the time:
select checksum(userid + rand())
or:
select checksum(convert(varchar(255), userid) + convert(varchar(255), getdate())
Both rand()
and getdate()
are guaranteed to be the same on all rows for for each expression they occur in. However, they will vary from query to query.
Upvotes: 1
Reputation: 7960
You can try (works for SQL Server 2016 or more):
ALTER Table travel_location
ALTER COLUMN userId ADD MASKED WITH (FUNCTION='Random(1,100000)')
The users who were not granted to unmask like below wouldn't see the real value after this change.
GRANT UNMASK TO TestUser;
Upvotes: 0