How do I trim or truncate a field in SQL? DOMAIN/USERNAME

Posted on 2007-07-22
Last Modified: 2007-11-27
How do I trim or truncate a field in SQL. For example...


The field domain name part 'domain/' (7 characters) is always the same, the username is of course always different. What command in SQL can I use to trim out the domain/ portion and end up with just the persons username?

Left(username, 2...)??


Any ideas?

Question by:a23m2000
    LVL 92

    Accepted Solution

    Hello a23m2000,

    SELECT SUBSTRING(username, 8, LEN(username) - 7) AS JustTheUserName
    FROM YourTable


    LVL 50

    Expert Comment

    Hello a23m2000,

    if its always 7 characters ... then use patricks

    other wise extend it to
    SELECT SUBSTRING(username, charindex('/',username)+1 , LEN(username) - charindex('/',username) ) AS JustTheUserName
    FROM YourTable


    LVL 11

    Expert Comment

    Not the nicest approach, but it wouldn't error out on records that have are not 'conform the norm'.

    (eg. if the record happens to have an empty username field, or if the username field contains "sa" or something)

    SELECT JustTheUserName = Replace(username, 'MYDOMAIN\', '')
      FROM YourTable

    But that would mean hardcoding the domain, and thus will give problems when
    * the domain name changes
    * another domain needs to be "filtered" too


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Introduced in Microsoft SQL Server 2005, the Copy Database Wizard ( is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Via a live example, show how to shrink a transaction log file down to a reasonable size.

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now