Link to home
Start Free TrialLog in
Avatar of RecipeDan
RecipeDan

asked on

Combine Two Date Fields

Hello:

I have two tables with date fields. Table1 has the date formatted like this: 2013-03-31 13:01:00. Table2 the date is formated like this 2013-03-31. How can I combine the two tables?

SELECT   Table1.ArrivalDateTime, Table2.MeetingDate
FROM         Table1 INNER JOIN
                      Table2 ON (CONVERT(date, Table1.ArrivalDateTime, 101) = Table2.MeetingDate

Open in new window

Avatar of Surendra Nath
Surendra Nath
Flag of India image

you can write it as below

SELECT   Table1.ArrivalDateTime, Table2.MeetingDate
FROM         Table1 
INNER JOIN   Table2 
ON DATEDIFF(dd,Table1.ArrivalDateTime,Table2.MeetingDate) = 0

Open in new window

That join is really not good, what if you have multiple arrivals and meetings on the same day.
Your result set will be invalid.
You need to create primary and foreign keys on your tables to implement the join
select table2.arrivaldatetime, table2.meetingdate from table1 inner join table2
on cast(floor(cast(table1.arrivaldatetime as float) ) as datetime) = table2.meetingdate
Avatar of RecipeDan
RecipeDan

ASKER

Awkingoo your suggestion shows as 2013-02-06 00:00:00.000. I need to delete the time in order to join the tables.
Do you just want to join on dates? Whats wrong with your query?
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia 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