SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get User Name from suser_name() and host_name()


Get User Name from suser_name() and host_name()

Author
Message
david.c.holley
david.c.holley
SSC Veteran
SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)

Group: General Forum Members
Points: 257 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?
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33636 Visits: 18560
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

Lowell
Lowell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29338 Visits: 39985
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!

david.c.holley
david.c.holley
SSC Veteran
SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)

Group: General Forum Members
Points: 257 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.
sqlbi.rahul
sqlbi.rahul
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
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
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33636 Visits: 18560
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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search