Marek Kwiendacz
Marek Kwiendacz

Reputation: 9814

How to get substring from given character?

I have table with logins in this form:

login1  
domain1\login2  
otherDomain\login3

Data is always in form:
DOMAIN\login
or
login

I need to retrieve user logins without prefixes in form (leave everything on right side of '\'):

login1  
login2  
login3

How to do that in SQL Server?

Upvotes: 1

Views: 372

Answers (1)

DaveShaw
DaveShaw

Reputation: 52788

You can do it like this:

print substring(@s, charindex('\', @s) + 1, len(@s));

This gets the index of the \ and then adds 1 to get the position after it, and performs a substring from that position to past the end of the string (which is OK in SQL 2008 R2).

Upvotes: 5

Related Questions