Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Combine Two Date Fields

Posted on 2013-06-25
6
Medium Priority
?
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ephraim Wangoya
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 41

Expert Comment

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

Accepted Solution

by:
PortletPaul earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

610 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