0

I would like to replace the all digits in a number with *, other than the last 4.

Here is my table:

Person
-----------
PersonID PersonBadgeNumber numeric(16,0)
1
  • Please post some example data, and before/after example, and correct grammar mistakes in question. (Clarify what "replace the all digits" means.) ty! Commented Jan 20, 2012 at 19:13

2 Answers 2

2

Something like this will work...

DECLARE @badgeId Numeric(16, 0) = 1238985495;
SELECT
  REPLICATE('*', LEN(@badgeId) - 4) +
  RIGHT(@badgeId, 4);
-- Produces: ******5495

To use it in a query do this:

SELECT
  REPLICATE('*', LEN(PersonBadgeNumber) - 4) +
  RIGHT(PersonBadgeNumber, 4) RedactedBadgeNumber
FROM Person;
Sign up to request clarification or add additional context in comments.

Comments

0

Another method is to use STUFF():

DECLARE @n numeric (16, 0);
SET @n = 1234567890123456;
SELECT STUFF(@n, 1, LEN(@n) - 4, REPLICATE('*', LEN(@n) - 4))

The above returns:

---------------- 
************3456 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.