May 20, 2010 at 11:56 am
SELECT REPLACE(SUSER_NAME(), HOST_NAME() + '\','') as userId
I need to pull out the user name from the fully qualified name ([domain name]/). The statement above isn't working as it fails to add the backslash to the host name.
What am I missing?
May 20, 2010 at 12:09 pm
david.c.holley (5/20/2010)
SELECT REPLACE(SUSER_NAME(), HOST_NAME() + '\','') as userId
I need to pull out the user name from the fully qualified name ([domain name]/). The statement above isn't working as it fails to add the backslash to the host name.
What am I missing?
The host_name will not be found in the suser_name function. You are looking for the string that matches host_name + \ inside the string of suser_name.
Are you trying to replace the user id with the host_name or with an empty string?
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
May 20, 2010 at 12:17 pm
i think this is what you might be after:
--results:
TheDomainTheUser
TheDomainlowell
declare @MyDomainLogin varchar(30)
SET @MyDomainLogin='TheDomain\lowell'
--SET @MyDomainlogin = SUSER_NAME()
SELECT
--the -1 and +1 are to remov ethe char '\' itself fromt eh results
SUBSTRING(@MyDomainLogin,1, CHARINDEX('\',@MyDomainLogin) -1) As TheDomain,
--find the location of the slash, grab substring of the right of THAT.
SUBSTRING(@MyDomainLogin,CHARINDEX('\',@MyDomainLogin) + 1,30) As TheUser
WHERE CHARINDEX('\',@MyDomainLogin) > 0--don't crash on a non-domain name
Lowell
May 20, 2010 at 1:43 pm
Maybe I should spend more time at work doing work and less time at home doing work. Since I'm running SQLServer Express on my laptop, the server name and host name are the same hence it works.
May 20, 2010 at 1:58 pm
try this
SELECT REPLACE(SUSER_NAME(), HOST_NAME() , '\','') as userId
i hope it works..
i have mine setup as
SELECT REPLACE(SUSER_NAME(), 'Dom-cs\','') as userId
May 20, 2010 at 2:15 pm
sqlbi.rahul (5/20/2010)
try thisSELECT REPLACE(SUSER_NAME(), HOST_NAME() , '\','') as userId
i hope it works..
i have mine setup as
SELECT REPLACE(SUSER_NAME(), 'Dom-cs\','') as userId
That will not work due to too many parameters passed to the replace function.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply