How do i truncate a nvarchar value like a social (123-45-6789) into just the last four digits?

Posted on 2009-02-20
Last Modified: 2012-05-06
Basically I'm trying to do what the title says. I'm using mssql 2005. I also have a query but it returns duplicite values. Well not really dups since the PK is different but it does have the same name values. Any easy way to get rid of that too? I'll include the query i'm running for that...
select distinct tbl_1.PK_P, tbl_1.LN, tbl_1.FN, from tbl_1

inner join archive_2 on tbl_1.LN = archive_2.Ln 

and tbl_1.FN = archive_2.Fn

and Vn is not null

Open in new window

Question by:jmvega00
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    please clarify with sample data what exactly you are trying to do...

    >I also have a query but it returns duplicite values
    there are most surely due to the join, with 1 to many relationship between the 2 tables.


    Author Comment

    assume: tbl_person with fields fname, lname, social
    lets say it only contains data: john, doe, 123-45-6789
    I want to truncate all the values in the social field so it only stores the last four digits so when I do a

    select social from tbl_person
    it only returns for example: 6789

    what update or comand would i have to run to achieve this truncate?
    LVL 142

    Accepted Solution

    UPDATE tbl_person
      SET social = RIGHT(social, 4)

    Author Comment

    Thanks! My head was fry'd and this is going into a pretty complex update query. Thanks again :)

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
    If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now