Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-07-22
3
Medium Priority
?
3,362 Views
Last Modified: 2007-11-27
How do I trim or truncate a field in SQL. For example...

domain/username

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...)??

substring??
Trim

Any ideas?

Thanks,
0
Comment
Question by:a23m2000
3 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 200 total points
ID: 19543762
Hello a23m2000,

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

Regards,

Patrick
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 19543966
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


 
Regards,

Lowfatspread
0
 
LVL 11

Expert Comment

by:deroby
ID: 19546111
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

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

810 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