March 5, 2012 at 7:34 pm
I need to write a report for HR. The report needs a whole lot of data including the last four of the SSN. However, they have thrown me a curve ball. They want the SSN to be displayed as XXX-XX-0000 (0000 = last four digits. I can get the last four, but when I use the replace function (REPLACE (P.SSN, 7, 'XXX-XX-'), it only replaces the number 7 with the X's. How do I drop the left 7, replace with XXX-XX-, then right the last four?
Thank you
March 5, 2012 at 8:12 pm
Never mind. I used a variable plus RIGHT
EXAMPLE:
DECLARE @mask VARCHAR(30)
SET @mask = 'XXX-XX-'
SELECT
p.FIRST_NAME + ' ' + p.LAST_NAME,
(SELECT @mask + RIGHT(p.SSN,4)) AS SSN
FROM
PERSON AS p
March 5, 2012 at 8:27 pm
I'm not doing anything on the backend (and sorry if I lead you to believe this). HR is requesting a report from me. I'm trying to mask the SSN on the report I dump to excel for them. We store the full SSN in the database, I want the report to show them XXX-XX-0000.
March 6, 2012 at 7:43 am
Another option is STUFF()
STUFF(p.SSN, 1, 7, 'XXX-XX-'
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
March 6, 2012 at 4:10 pm
SQL_Enthusiast-AZ (3/5/2012)
Never mind. I used a variable plus RIGHTEXAMPLE:
DECLARE @mask VARCHAR(30)
SET @mask = 'XXX-XX-'
SELECT
p.FIRST_NAME + ' ' + p.LAST_NAME,
(SELECT @mask + RIGHT(p.SSN,4)) AS SSN
FROM
PERSON AS p
or without the variable:
select 'xxx-xx-' + right(p.ssn,4) as SSN
March 6, 2012 at 5:06 pm
Thank you. That's a lot easier...!
March 6, 2012 at 6:10 pm
easy = good !
😀
March 6, 2012 at 9:26 pm
CELKO (3/5/2012)
Why are you formatting data in the back end? The basic principle of a tiered architecture is that display is done in the front end and never in the back end. This is a more basic programming principle than just SQL and RDBMS.
If you don't do such a thing on the backend, then you're transmitting clear text SSN's over the wire where anyone with a smart phone can intercept them. 😉
The truth is, they shouldn't be storing the SSN in clear text to being with but that's a whole 'nuther story.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 6, 2012 at 9:28 pm
SQL_Enthusiast-AZ (3/5/2012)
I need to write a report for HR. The report needs a whole lot of data including the last four of the SSN. However, they have thrown me a curve ball. They want the SSN to be displayed as XXX-XX-0000 (0000 = last four digits. I can get the last four, but when I use the replace function (REPLACE (P.SSN, 7, 'XXX-XX-'), it only replaces the number 7 with the X's. How do I drop the left 7, replace with XXX-XX-, then right the last four?Thank you
Now that you have your answer, you should advise someone that they should not be storing SSN's in clear text to begin with.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply