Solved

Combine Two Date Fields

Posted on 2013-06-25
6
336 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 32

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

825 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