SQL Function

Hi, I have the following code from a dll which our sql function calls to join Names. This Dll is part of SQL assembly. We would like stop using the assembly and instead use this code within the function. Is there way to convery this to SQL script.

HERE IS HOW Current SQL function calls the EXTERNAL assembly

ALTER FUNCTION [dbo].[JoinName](@FirstName [nvarchar](80), @MiddleName [nvarchar](80), @LastName [nvarchar](80), @Suffix [nvarchar](80))
EXTERNAL NAME [JoinName].[JoinName.UserDefinedFunctions].[JoinName]
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server

Partial Public Class UserDefinedFunctions
    Private Const MULTIPLE_SPACE_PATTERN As String = "\s{2,}"
    Private Const SINGLE_SPACE_REPLACEMENT As String = " "
    Private Const TRAILING_COMMA_PATTERN As String = "\,$"
    Private Const EMPTY_STRING_REPLACEMENT As String = ""
    Private Const LAST_NAME_PRESENT_FORMAT As String = "{0}, {1} {2} {3}"
    Private Const LAST_NAME_ABSENT_FORMAT As String = "{0} {1} {2}"
    <Microsoft.SqlServer.Server.SqlFunction(DataAccess:=DataAccessKind.None, _
                                            SystemDataAccess:=SystemDataAccessKind.None, _
                                            IsDeterministic:=True, _
                                            IsPrecise:=True)> _
    Public Shared Function JoinName(ByVal FirstName As SqlString, _
                                    ByVal MiddleName As SqlString, _
                                    ByVal LastName As SqlString, _
                                    ByVal Suffix As SqlString) As SqlString

        Dim JoinedName As SqlString
        Dim JoinedNameAsString As String

        If FirstName.IsNull Then
            FirstName = ""
        End If

        If MiddleName.IsNull Then
            MiddleName = ""
        End If

        If LastName.IsNull Then
            LastName = ""
        End If

        If Suffix.IsNull Then
            Suffix = ""
        End If

        If (LastName <> "") Then
            JoinedNameAsString = String.Format(LAST_NAME_PRESENT_FORMAT, LastName, FirstName, MiddleName, Suffix)
            JoinedNameAsString = String.Format(LAST_NAME_ABSENT_FORMAT, FirstName, MiddleName, Suffix)
        End If

        JoinedName = Trim(System.Text.RegularExpressions.Regex.Replace(input:=JoinedNameAsString, _
                                                                       pattern:=MULTIPLE_SPACE_PATTERN, _
                                                                       replacement:=SINGLE_SPACE_REPLACEMENT, _

        JoinedName = Trim(System.Text.RegularExpressions.Regex.Replace(input:=JoinedName.ToString, _
                                                                       pattern:=TRAILING_COMMA_PATTERN, _
                                                                       replacement:=EMPTY_STRING_REPLACEMENT, _

        Return JoinedName
    End Function

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
what does this function do? any sample?
vb7guyAuthor Commented:
This Function Joins Name (formal Name) and returns a string. For example
if I  pass the following name to the function
Firstname: TOBIN      LastName: SMITH      MiddleName: L            

it should return

Also, if any of the passing field has null value or spaces it also truncates the spaces
HainKurtSr. System AnalystCommented:
create a simple function like this and use it in your sql
alter function JoinName(
 @FirstName Varchar(100),
 @MiddleName Varchar(100),
 @LastName Varchar(100),
 @Suffix Varchar(100)
) returns varchar(400) as
  declare @Result Varchar(400) ='';
    --Private Const LAST_NAME_PRESENT_FORMAT As String = "{0}, {1} {2} {3}"
    --Private Const LAST_NAME_ABSENT_FORMAT As String = "{0} {1} {2}"
  if @LastName = ''
    set @result = @Firstname + ' ' + @MiddleName + ' ' + @Suffix;
    set @result = @Lastname + ', ' + @Firstname + ' ' + @MiddleName + ' ' + @Suffix;
  return RTrim(LTrim(Replace(@Result, '  ', ' ')));

select dbo.JoinName('Hain', 'Kurt', '', 'Mr.')
Hain Kurt Mr.

Open in new window

HainKurtSr. System AnalystCommented:
some more tweaks may be required... but you should get the point...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vb7guyAuthor Commented:
Answer was partial. but I figured out the rest
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.