Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Data formatting SSN

From Wiki

Jump to: navigation, search

You should not store unencrypted SSNs in the database. If you do store them and you don't have the dashes but you need to display them then you can use the STUFF function to accomplish that Here is an example, the output will be 111-22-3333

  1. DECLARE @SSN VARCHAR(11)
  2.     SELECT @SSN ='111223333'
  3.  
  4.     SELECT STUFF(STUFF(@SSN,4,0,'-'),7,0,'-')


You can also use STUFF to hide the last 4 characters for example.

  1. DECLARE @SSN VARCHAR(11)
  2.     SELECT @SSN ='111223333'
  3.  
  4.     SELECT STUFF(@SSN,6,4,'----')


Of course you could have LEFT instead

  1. DECLARE @SSN VARCHAR(11)
  2.     SELECT @SSN ='111223333'
  3.  
  4.     SELECT LEFT(@SSN,5) +'----'


Contributed by: --SQLDenis 02:49, 31 May 2008 (GMT)

Part of SQL Server Programming Hacks

Section Handy tricks

172 Rating: 1.9/5 (9 votes cast)