tags:

views:

230

answers:

2

I have a User_Id column with data DOMAIN\USERID

I need it to be DOMAIN\userid

Whats the (sql server) sql?

Update: Answer from @David McEwing

update UserTable 
set [User_Id] = SUBSTRING( [User_Id], 0, CHARINDEX('\',  [User_Id])+1) 
        + lower(SUBSTRING( [User_Id], CHARINDEX('\',  [User_Id])+1, len( [User_Id])))
+3  A: 
SELECT LEFT(User_Id, CHARINDEX('\', User_Id)) -- grabs DOMAIN and '\'
+ LOWER(RIGHT(User_Id, 
      LEN(User_Id) - CHARINDEX('\', User_Id))) -- concat with lower of userid
FROM MyTable
Jason
sorry this didnt work... it was missing a closing bracket, plus if the the userid is longer than the domain name then the userid is concatenated
Paul Rowland
@Paul: To your first point, guilty as charged and fixed. To your second point, huh?
Jason
@Paul: Huh? I thought this solution was better than mine. Weird how my mind thinks in substrings now rather than left and right due to them not being present in C#.
David McEwing
@David McEwing: With extension methods they are. :-)
Jason
@Jason: current answer works as well, the previous version of your answer without the LEN in the RIGHT function wasnt working, I guess you were editing your answer at about the time I was commenting.. thanks for the answer.
Paul Rowland
+2  A: 

This should do the trick:

declare @name varchar(50)
set @name= 'DOMAIN\USERID'

select SUBSTRING(@name, 0, CHARINDEX('\', @name)+1) 
     + lower(SUBSTRING(@name, CHARINDEX('\', @name)+1, len(@name)))
David McEwing
this worked thanks
Paul Rowland