Replacing important missing data by row

  • This is my first post in many years so please forgive if this is the wrong forum. Been a DBA in a previous life and am now working with a client on a data feed and this issue has raised it's ugly head. The issue is we have a table that has SSNs in a data field, but not all rows have an SSN (the UI has the field as optional) so some users enter it and some don't, painful but true. What I want to do is where I have multiple rows and where the lastName, firstName and date of birth (DOB) are a match with another row and one of those rows have a valid SSN, I want to enter that valid SSN into that blank SSN field. In essence I would be entering the optional SSN in those rows where the user chose not to but for some reason had done it in the past or on a future entry.

    Example:

    ID lastName firstName DOB SSN other columns...

    1 Doe John 1/1/1967 111-22-3333

    2 Doe John 1/1/1967

    3 Doe John 1/1/1967 111-22-3333

    I am sure there is a elegant way to do this and while I have started poking at it, I thought I would hit some forums and look for help. Understood this might need some programming but if I can at least select the rows to start with the client can appreciate the counts and we can fix.

    Appreciate any help

    Steve Z

  • I'm not sure you should do this as it might give you incorrect results if there are several people with same name and DOB (chances are low, but it could happen).

    If you proceed with this, you could use this sample code to guide you.

    DECLARE @test-2 TABLE(

    ID int,

    lastName varchar(20),

    firstName varchar(20),

    DOB date,

    SSNvarchar(20))

    INSERT @test-2

    VALUES

    (1, 'Doe', 'John', '19670101', '111-22-3333'),

    (2, 'Doe', 'John', '19670101', NULL),

    (3, 'Doe', 'John', '19670101', '111-22-3333')

    UPDATE t1 SET

    SSN = t2.SSN

    FROM @test-2 t1

    JOIN @test-2 t2 ON t1.lastName = t2.lastName

    AND t1.firstName = t2.firstName

    AND t1.DOB = t2.DOB

    AND t2.SSN IS NOT NULL

    WHERE t1.SSN IS NULL

    SELECT * FROM @test-2

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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