compare data and update the table

Hi Team,

1. i am having 2 table to comapre the data with patient1 and patient2
fields have excatly same in both table,suppose having multiple data like 10 rows in both table

we have to check for current month only .
if exactly same patient records present ,then upd_date of patient1 table upadate with current date.

Patient1table :    patient_id   firstname  lastname upd_date
patient2table:     patient_id   firstname  lastname upd_date

plz help
Who is Participating?
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.

Deepak SubburajanDB DeveloperCommented:
Its simple. If I got you correctly, here you go,

   upd_date = Getdate()
   Patient1 P1,
   Patient2 P2
   P1.patient_id = P2.patient_id

Open in new window

you also have mentioned as you want to do this only for the current month. Include the column in the WHERE clause. You are done.
Vikas GargBusiness Intelligence DeveloperCommented:

Update patient2table
SET upd_date = GETDATE()
WHERE Month(upd_date) = Month(Getdate())
and patient_id  IN (Select patient_id  from Patient1table)

Open in new window

Habib PourfardSoftware DeveloperCommented:
You could write:
UPDATE Patient1table 
SET upd_date = GETDATE()
FROM Patient1table P1 INNER JOIN Patient2table P2 ON P1.patient_id = P2.patient_id
AND P1.firstname = P2.firstname  
AND P1.lastname = P2.lastname 
AND P1.upd_date = P2.upd_date
WHERE DATEPART(m, P1.upd_date) = DATEPART(m, DATEADD(m, -1, getdate()))
AND DATEPART(yyyy, P1.upd_date) = DATEPART(yyyy, DATEADD(m, -1, getdate()))

Open in new window

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nicemanishAuthor Commented:
Thanks for reply
a little bit add can we do also
"when we have a patient record in patient2, which is not present in patient1.i want to be copy that data in patient 1 as a new record"
Habib PourfardSoftware DeveloperCommented:
INSERT INTO Patient1table (firstname, lastname, ...)
Select P2.firstname, P2.lastname, ...
FROM Patient1table P1 RIGHT JOIN Patient2table P2 ON P1.patient_id = P2.patient_id
WHERE P1.patient_id IS NULL

Open in new window


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
nicemanishAuthor Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.