Solved

Combine Two Date Fields

Posted on 2013-06-25
6
335 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

920 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

16 Experts available now in Live!

Get 1:1 Help Now