1

I have a set of row in my database that look like this:

Mr Joe Bloggs
Joe Bloggs
Joe Jim Bloggs
Joe Jim David Bloggs
Mr Joe Jim Bloggs
Mr Joe Jim David Bloggs

Output required:

Title | First Name | Middle Name | Surname
------------------------------------------
Mr | Joe | x | Bloggs
x | Joe | x | Bloggs
x | Joe | Jim | Bloggs
x | Joe | Jim David | Bloggs
Mr | Joe | Jim | Bloggs
Mr | Joe | Jim David | Bloggs

This will be run in a SQL Stored Procedure. What i need is this output generated while looping through a CURSOR and the 4 variables to update the 4 columns in the database. But as you can see the data is in 1 column and varies.

I am finding it hard to understand the best way of doing this and would like your help please. Let me know if you need anything else.

Similar Questions to mine but i feel these dont give me the full answer i need or i think it doesnt answer my question:

EDIT:

    --Temporary Table to Merge the full name together in order to split by comma. Each split will have a unique record but with the same address information for each name. 
    CREATE TABLE #TempOutput
    (
        ClientReference varchar(MAX),
        Prefix varchar(MAX),
        Reference int,
        MergedFullName varchar(MAX),
        FormatName varchar(MAX),
        Title varchar(MAX),
        FirstName varchar(MAX),
        MiddleName varchar(MAX),
        Surname varchar(MAX)
    )

    DECLARE @Ref varchar(MAX)
    DECLARE @Prefix varchar(MAX)
    DECLARE @Reference int
    DECLARE @MergedName varchar(MAX)
    DECLARE @FormatName varchar(MAX)
    DECLARE @Title varchar(MAX)
    DECLARE @FirstName varchar(MAX)
    DECLARE @MiddleName varchar(MAX)
    DECLARE @Surname varchar(MAX)

    DECLARE ExtraNameSplitCursor CURSOR FOR
        SELECT 
            [Client Reference], 
            Prefix,
            Reference,
            CASE 
                WHEN LEFT(Surname,3) = 'Mr,' THEN (Title + ' ' + [First Name] + ' ' + [Middle Name] + ' ' + Replace(Surname,'Mr,',''))
                WHEN LEFT(Surname,4) = 'Mrs,' THEN (Title + ' ' + [First Name] + ' ' + [Middle Name] + ' ' + Replace(Surname,'Mrs,',''))
                ELSE (Title + ' ' + [First Name] + ' ' + [Middle Name] + ' ' + Surname) 
            END AS [Merged Full Name]
        FROM #StartResults

    OPEN ExtraNameSplitCursor

    FETCH NEXT FROM ExtraNameSplitCursor INTO @Ref, @Prefix, @Reference, @MergedName
    WHILE @@FETCH_STATUS = 0
        BEGIN
            INSERT INTO #TempOutput (ClientReference, Prefix, Reference, MergedFullName) 
            SELECT @Ref, @Prefix, @Reference, GUID, FROM dbo.fn_SPLIT_LIST_STRING (@MergedName)

            --SELECT CASE 
            --  WHEN @FormatName.GUID = 'Mr ' THEN @Title = 'Mr'
            --  WHEN LEFT(@MergedName,4) = 'Mrs ' THEN @Title = 'Mrs' 
            --  WHEN LEFT(@MergedName,5) = 'Miss ' THEN @Title = 'Miss'
            --  WHEN LEFT(@MergedName,3) = 'Ms ' THEN @Title = 'Ms'
            --  WHEN LEFT(@MergedName,3) = 'Dr ' THEN @Title = 'Dr'
            --  WHEN LEFT(@MergedName,4) = 'Rev ' THEN @Title = 'Rev'
            --  WHEN LEFT(@MergedName,5) = 'Lord ' THEN @Title = 'Lord'
            --  ELSE @Title = '' 
            --END AS Title,
            --CASE 
            --  WHEN MID(@MergedName,2,1) = ' ' THEN @FirstName = '' ELSE @FirstName = CHARINDEX(' ', MergedFullName + ' ') FROM #TempOutput AS FirstName

            FETCH NEXT FROM ExtraNameSplitCursor INTO @Ref, @Prefix, @Reference, @MergedName, @FlatNo, @HouseName, @HouseNo, @Street, @District, @PostTown, @County, @PostCode
        END

    CLOSE ExtraNameSplitCursor
    DEALLOCATE ExtraNameSplitCursor

    --Select statement for temporary table
    SELECT 
        ISNULL(ClientReference,'') AS ClientReference,
        ISNULL(Prefix,'') AS Prefix,
        ISNULL(Reference,'') AS Reference,
        ISNULL(MergedFullName,'') AS MergedFullName, 
        ISNULL(Title,'') AS Title,
        ISNULL(FirstName,'') AS FirstName,
        ISNULL(MiddleName,'') AS MiddleName,
        ISNULL(Surname,'') AS Surname

    FROM #TempOutput Result
    ORDER BY ClientReference
    for xml auto

    --Drop all temporary tables
    DROP TABLE #TempOutput
    DROP TABLE #StartResults

END
Community
  • 1
  • 1
Naxos
  • 45
  • 2
  • 9
  • 9
    How are you supposed to know what word falls into which category? For your last example, how do we know that `Bloggs` isn't a first name, and that `Limited` isn't the last name? – Michael Fredrickson Nov 27 '12 at 16:38
  • 2
    It is good to know that you have searched for an answer already but what code have you written to accomplish this task. Please post what you have tried? – Taryn Nov 27 '12 at 16:39
  • This would be a great question for SQL Fiddle: http://sqlfiddle.com/ – Nick Vaccaro Nov 27 '12 at 17:11
  • @MichaelFredrickson - Fair play Michael, for this case i have removed this from the example dataset. This is a very rare occurance and i will update this manually. – Naxos Nov 28 '12 at 14:50
  • @bluefeet I have added a code example of what im trying to do. Thought that i need to detect the Title and then i been thinking i would try to sort out the rest with split by spaces maybe? – Naxos Nov 28 '12 at 14:54
  • @Norla I did try to copy my code to SQLFiddle and while it works for me on Microsoft SQL Server Management Studio 2008 R2 i couldnt get it to work on there complaining about an incorrect syntax. I will look into this further and try to get into the same working format as i have then i will pass the link on. – Naxos Nov 28 '12 at 14:55

1 Answers1

0

I have resolved my issue and thanks for the clues and help.

I have basically added a 2nd cursor which does the following:

  • Grab the Merged Full Name field and put into a temporary variable.
  • Checks the title by running a case statement to check Mr, Mrs etc. else enter blank string into Title.
  • Update Title Trim the title of the Merged Full Name
  • Check the surname by RIGHT(@TempName, CHARINDEX(' ', REVERSE(@TempName)) - 1) and assume that this is the surname.
  • Update Surname Trim and Replace this of the Merged Full Name
  • Check the left for First Name by LEFT(@TempName, CHARINDEX(' ',
    @TempName + ' ') -1)
  • Update First Name Trim and Replace this of the Merged Full Name
  • Anything else is put in the middle name.

Bit long winded but in 99.9% cases in my database it formats pefectly.

If you would like the above code let me know.

Thank you for you time.

Naxos
  • 45
  • 2
  • 9