Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Get User Name from suser_name() and host_name() Expand / Collapse
Author
Message
Posted Thursday, May 20, 2010 11:56 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 26, 2015 10:16 AM
Points: 113, Visits: 191

SELECT REPLACE(SUSER_NAME(), HOST_NAME() + '\','') as userId


I need to pull out the user name from the fully qualified name ([domain name]/[user id]). The statement above isn't working as it fails to add the backslash to the host name.

What am I missing?
Post #925423
Posted Thursday, May 20, 2010 12:09 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 19,918, Visits: 18,133
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]/[user id]). 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
Post #925435
Posted Thursday, May 20, 2010 12:17 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:47 PM
Points: 14,437, Visits: 37,834
i think this is what you might be after:

--results:
TheDomain TheUser
TheDomain lowell

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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #925441
Posted Thursday, May 20, 2010 1:43 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 26, 2015 10:16 AM
Points: 113, Visits: 191
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.
Post #925482
Posted Thursday, May 20, 2010 1:58 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, May 5, 2011 2:15 PM
Points: 92, Visits: 111
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
Post #925491
Posted Thursday, May 20, 2010 2:15 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 19,918, Visits: 18,133
sqlbi.rahul (5/20/2010)
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


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
Post #925498
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse