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
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
HI,
Update patient2table
SET upd_date = GETDATE()
WHERE Month(upd_date) = Month(Getdate())
and patient_id IN (Select patient_id from Patient1table)
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()))
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"
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
excellent
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.