Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: Monday, September 20, 2010 6:12 AM
Points: 105, Visits: 172

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


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Friday, August 29, 2014 1:51 PM
Points: 21,644, Visits: 15,317
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #925435
Posted Thursday, May 20, 2010 12:17 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:38 AM
Points: 12,910, Visits: 32,021
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
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: Monday, September 20, 2010 6:12 AM
Points: 105, Visits: 172
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


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Friday, August 29, 2014 1:51 PM
Points: 21,644, Visits: 15,317
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #925498
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse