Link to home
Start Free TrialLog in
Avatar of nicemanish
nicemanish

asked on

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
Avatar of Deepak Subburajan
Deepak Subburajan

Its simple. If I got you correctly, here you go,

UPDATE
   Patient1
SET
   upd_date = Getdate()
FROM
   Patient1 P1,
   Patient2 P2
WHERE
   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.
Avatar of Vikas Garg
HI,

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

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

Avatar of nicemanish

ASKER

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"
ASKER CERTIFIED SOLUTION
Avatar of Habib Pourfard
Habib Pourfard
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
excellent