Joining on date only

  • I am trying to join these two tables based on date only, for instance, 05-03-17


    SELECT top 5 CreateDate
    FROM [dbo].[Tracking] 
    JOIN [dbo].[SalesOrderXmit] ON CreateDate=Entered

    Above is returning no set at all, of course.

    How can I accomplish that, without torpedoing any existing Index? I know I must use a function in order to get rid of the hr, ss, ms, etc.

  • Please post the DDL for the tables involved, sample data and expected output. It will help the experts here to help you out.

  • TheSQL_fan - Wednesday, May 31, 2017 7:05 AM

    I am trying to join these two tables based on date only, for instance, 05-03-17


    SELECT top 5 CreateDate
    FROM [dbo].[Tracking] 
    JOIN [dbo].[SalesOrderXmit] ON CreateDate=Entered

    Above is returning no set at all, of course.

    How can I accomplish that, without torpedoing any existing Index? I know I must use a function in order to get rid of the hr, ss, ms, etc.

    SELECT TOP(5) x.CreateDate
    FROM [dbo].[Tracking] t
    JOIN [dbo].[SalesOrderXmit] x
     ON CAST(x.CreateDate AS DATE) = CAST(t.Entered AS DATE)
    ORDER BY [shouting]

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work - Wednesday, May 31, 2017 7:19 AM

    TheSQL_fan - Wednesday, May 31, 2017 7:05 AM

    I am trying to join these two tables based on date only, for instance, 05-03-17


    SELECT top 5 CreateDate
    FROM [dbo].[Tracking] 
    JOIN [dbo].[SalesOrderXmit] ON CreateDate=Entered

    Above is returning no set at all, of course.

    How can I accomplish that, without torpedoing any existing Index? I know I must use a function in order to get rid of the hr, ss, ms, etc.

    SELECT TOP(5) x.CreateDate
    FROM [dbo].[Tracking] t
    JOIN [dbo].[SalesOrderXmit] x
     ON CAST(x.CreateDate AS DATE) = CAST(t.Entered AS DATE)
    ORDER BY [shouting]

    Thanks Chris

    That's what I had. So this won't torpedo any Index I may have on the joining attributes? Because the CAST function?

  • TheSQL_fan - Wednesday, May 31, 2017 7:31 AM

    ChrisM@Work - Wednesday, May 31, 2017 7:19 AM

    TheSQL_fan - Wednesday, May 31, 2017 7:05 AM

    I am trying to join these two tables based on date only, for instance, 05-03-17


    SELECT top 5 CreateDate
    FROM [dbo].[Tracking] 
    JOIN [dbo].[SalesOrderXmit] ON CreateDate=Entered

    Above is returning no set at all, of course.

    How can I accomplish that, without torpedoing any existing Index? I know I must use a function in order to get rid of the hr, ss, ms, etc.

    SELECT TOP(5) x.CreateDate
    FROM [dbo].[Tracking] t
    JOIN [dbo].[SalesOrderXmit] x
     ON CAST(x.CreateDate AS DATE) = CAST(t.Entered AS DATE)
    ORDER BY [shouting]

    Thanks Chris

    That's what I had. So this won't torpedo any Index I may have on the joining attributes? Because the CAST function?

    I'm fairly sure that casting from datetime to date is SARGable on supported versions of SQL Server. Glance at your execution plan to confirm.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I am using SQL2008, but I will take a look on the actual execution plan. Thank you!

  • TheSQL_fan - Wednesday, May 31, 2017 7:31 AM

    Thanks Chris

    That's what I had. So this won't torpedo any Index I may have on the joining attributes? Because the CAST function?

    Under normal circumstances, yes, using a function on a table column in the search predicate would affect SARGability. The CAST/CONVERT from datetime to date is a rare "exception to the rule".

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply