Link to home
Start Free TrialLog in
Avatar of a23m2000
a23m2000Flag for United States of America

asked on

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

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,
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of deroby
deroby

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