?
Solved

Combine Two Date Fields

Posted on 2013-06-25
6
Medium Priority
?
339 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.
Suggested Courses

762 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