Get firstname and lastname from domain account

  • Hi,

    As title really!

    DOMAIN\Joe.Bloggs should be Firstname: Joe and Lastname: Bloggs

    I'm currently doing lots of funky substrings:

    select        substring(SYSTEM_USER, charindex('\', SYSTEM_USER, 0) +1, charindex('.', SYSTEM_USER) - charindex('\', SYSTEM_USER, 0) -1),
                    substring(SYSTEM_USER, charindex('.', SYSTEM_USER, 0) +1, len(SYSTEM_USER) - charindex('.', SYSTEM_USER, 0));


    Is there a neater way?!

    Thanks!

  • DECLARE @SYSTEM_USER sysname = 'DOMAIN\Joe.Bloggs';
    WITH SystemUser (UserName) AS (
        SELECT REPLACE(@SYSTEM_USER,'\','.')
        )
    SELECT
         PARSENAME(UserName,2) AS FirstName
    ,    PARSENAME(UserName,1) AS LastName
    FROM SystemUser;

    John

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply