Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Replacing important missing data by row Expand / Collapse
Author
Message
Posted Thursday, July 25, 2013 11:09 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 4:05 PM
Points: 1, Visits: 21
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
Post #1477635
Posted Thursday, July 25, 2013 11:24 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 5:23 PM
Points: 4,049, Visits: 9,214
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 TABLE(
ID int,
lastName varchar(20),
firstName varchar(20),
DOB date,
SSN varchar(20))
INSERT @Test
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 t1
JOIN @Test 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




Luis C.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1477645
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse