a23m2000
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
(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
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