I need help removing a leading 0 in my query

Hi Experts,
I need to create a query that will return a list of disctint Social Security Numbers from my Students table.  The problem I have is that some of the Social Security Numbers in my SSN column have a leading 0.  My query needs to check for any Social Security Numbers with a length > 9 and removed the leading 0. How can I do this?

Thanks in advance,
mrotor
mainrotorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
SELECT RIGHT(SSN, 9) as ssn from yourTable
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
select case when len(ssn) > 9 then substring(ssn,2,9) else ssn end
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>The problem I have is that some of the Social Security Numbers in my SSN column have a leading 0.
For starters, this implies that the SSN column is a char field of some sort, and not numeric, as numeric columns do not support leading zeros.

>check for any Social Security Numbers with a length > 9 and removed the leading 0. How can I do this?

SELECT SSN as ssn_greater_than_nine_characters
FROM YourTable
WHERE LEN(CAST(CAST(SSN as bigint) as varchar(25)) ) > 9
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
For example..
CREATE TABLE #ssn (ssn varchar(25))

INSERT INTO #ssn (ssn) VALUES('123456789'), ('012345678'), ('00000000001'), ('12345678912') 

SELECT SSN as ssn_greater_than_nine_characters
FROM #ssn
WHERE LEN(CAST(CAST(SSN as bigint) as varchar(25)) ) > 9 

Open in new window

0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Just curious ... how does the solution that was accepted handle leading zeros?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.