Solved

Combine Two Date Fields

Posted on 2013-06-25
6
334 Views
Last Modified: 2013-06-26
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

0
Comment
Question by:RecipeDan
6 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39275489
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

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 39275563
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
0
 
LVL 31

Expert Comment

by:awking00
ID: 39275946
select table2.arrivaldatetime, table2.meetingdate from table1 inner join table2
on cast(floor(cast(table1.arrivaldatetime as float) ) as datetime) = table2.meetingdate
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:RecipeDan
ID: 39276138
Awkingoo your suggestion shows as 2013-02-06 00:00:00.000. I need to delete the time in order to join the tables.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39276502
Do you just want to join on dates? Whats wrong with your query?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39276839
SELECT
          Table1.ArrivalDateTime
        , Table2.MeetingDate
FROM Table1
INNER JOIN Table2 ON CONVERT(DATE, Table1.ArrivalDateTime) = Table2.MeetingDate

You do not need the ,'101'

The only concern here is that applying a function to the datetime field may remove use of indexing you may have on ArrivalDateTime. Opinions differ on this e.g. (see this forum)
Does SQL 2008 also have an optimization such that
Where convert(date, aDateTimeColumn) = '20101020'
will get optimized to an INDEX SEEK rather than an scan?

Yes, it can do a SEEK for above operation.
Tibor Karaszi, SQL Server MVP
I would suggest you inspect the execution plan to see if it is causing table or index scanning.

& some semantics (sorry):

you have 2 fields, one appears to be datetime, the other appears to be date
date and datetime fields are seen in a format - but not stored in a format - hence what they look like doesn't really matter. What does matter is the actual data type of each field.

oh, and an alternative to using convert(date,....) which is efficient is:
SELECT
          Table1.ArrivalDateTime
        , Table2.MeetingDate
FROM Table1
INNER JOIN Table2 ON dateadd(day, datediff(day,0, Table1.ArrivalDateTime ), 0) = Table2.MeetingDate

Open in new window

the combination of datediff with dateadd strips time to 00:00:00

the notes above regarding indexing still apply of course.
{+2 edits sorry}
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now