Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Replacing important missing data by row


Replacing important missing data by row

Author
Message
steve.zimmerman
steve.zimmerman
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 22
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
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8554 Visits: 18142
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.
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search