Skip to content

Instantly share code, notes, and snippets.

@sarangbk
Last active June 20, 2016 22:10
Show Gist options
  • Save sarangbk/007a2d9cbf7eddbb234201ff66657500 to your computer and use it in GitHub Desktop.
Save sarangbk/007a2d9cbf7eddbb234201ff66657500 to your computer and use it in GitHub Desktop.
-- Convert Down Level Logon format e.g. domain\username to User Principal Name (UPN) format [email protected]
-- Convert Down Level Logon format e.g. domain\username to User Prindipal Name (UPN) format [email protected]
-- The formats are explained here https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v=vs.85).aspx
IF OBJECT_ID (N'[dbo].[uFnGetDomainAtFromDomainSlash]', N'FN') IS NOT NULL
DROP FUNCTION uFnGetDomainAtFromDomainSlash;
GO
CREATE FUNCTION [dbo].[uFnGetDomainAtFromDomainSlash](@AccountNameWithDomainSlash VARCHAR(MAX), @DomainExtension VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @AccountName VARCHAR(MAX),
@DomainName VARCHAR(MAX)
SET @AccountName = SUBSTRING ( @AccountNameWithDomainSlash, PATINDEX('%\%',@AccountNameWithDomainSlash) + 1, 100)
SET @DomainName = SUBSTRING ( @AccountNameWithDomainSlash, 0, PATINDEX('%\%',@AccountNameWithDomainSlash))
RETURN CONCAT(@AccountName, '@', @DomainName, @DomainExtension)
END;
GO
-- Usage
-- This will convert 'wayne-industries\bruce.wayne' to '[email protected]'
SELECT [dbo].[uFnGetDomainAtFromDomainSlash]('wayne-industries\bruce.wayne', '.gotham')AS AccountNamwWithDomain
@knightWorm
Copy link

you should add a preview of what you want to create that way i will be useful tdo that we can understand..

@sarangbk
Copy link
Author

@ynotcme01000 thanks, good point, done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment