tags:

views:

44

answers:

2

I have a string in the following format 'DOMAIN\username'.

I wish to trim the domain and the backslash from the string. I cannont simply use RIGHT() as we have multiple domains of varying length.

What is the most portable way to do this?

Thank you.

+4  A: 

Select Right('DOMAIN\UserName', Len('DOMAIN\UserName') - Charindex('\', 'DOMAIN\UserName'))

Barry
Why the downvote?
Barry
Probably because this produces incorrect results.
LukeH
@LukeH - so it does. Couldn't see that for looking. I've amended my answer now. Thanks.
Barry
@Barry: Still broken! It coinicidentally works for this specific example, but what about `ANOTHERDOMAIN\username` or `DOMAIN\anotherusername`?
LukeH
@LukeH - Wow, my brain is just not switched on today. Sorted it now.
Barry
+2  A: 

you can try:

select substring('DOMAIN\username',  charindex('\' ,'DOMAIN\username')+1, len('DOMAIN\username'))
anishmarokey