Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Querying data by joining two tables in two database on same servers

There are two tables in two different databases in same server. database table are as below.

enter image description here

I want to get records of table 2 where delivery_date (date only. It means 2023-01-25) is equal to logtime date in table 1. Delivery_date and logtime datetype is datetime. Result should be as below.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I tried as below

SELECT * 
FROM Database2.table2  t2 
INNER JOIN Database1.table1  t1 
WHERE t2.delivery_date= t1.logtime AND t2.v_id='A001';

This is not getting what I want. how can I solve this.

>Solution :

You have to convert both date time values to date using the DATE-function:

SELECT * 
FROM Database2.table2  t2 
INNER JOIN Database1.table1  t1 
on DATE(t2.delivery_date)= Date(t1.logtime) AND t2.v_id='A001';

If you do not do it, the complete date time value will be compared

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading