Search code examples
sqlsql-serverdatetime2

SQL ignore nanoseconds when comparing datetime2


I have 2 tables with a datetime2 field each. One of the tables has stored nanoseconds as well, while the other one only stores up to milliseconds (there is no way to change this data). Now I want to compare the 2 tables and check if the datetime2 values match, but I have to ignore the nanoseconds for this.

Sample data:

Table1                         Table2

2018-01-24 10:51:23.9976180    2018-01-24 10:51:23.9970000
2018-01-24 10:51:23.9976180    2018-01-24 10:51:23.9970000
2018-01-24 11:08:23.2578500    2018-01-24 11:08:23.2570000

my query currently looks like this:

select t1.* from Table1 t1, Table12 t2 where t1.tradeDateTime = t2.tradeDate

As you can guess, I am not getting any results for these example, because the nanoseconds differ.

So my question is, how can I ignore the nanoseconds in the where-clause?

Edit:

I know that I should use proper JOINs. This query was just to test if I made no mistakes in my query using JOIN


Solution

  • This all depends on what precision you want to work with. Do you want to match just the date portion or the date and time without milliseconds?

    Here's a few ways to change the precision of the data:

    CREATE TABLE #dates ( val DATETIME2 );
    
    INSERT INTO #dates ( val ) VALUES ( SYSDATETIME());   
    INSERT INTO #dates ( val ) VALUES ( SYSDATETIME());    
    INSERT INTO #dates ( val ) VALUES ( SYSDATETIME());
    
    SELECT d.val AsOriginalValue ,
           CAST(d.val AS DATETIME) AsDateTime ,
           CAST(d.val AS DATETIME2(0)) AsDateTime2_0 ,
           CAST(d.val AS DATE) AsDate
    FROM   #dates AS d;
    
    DROP TABLE #dates;
    

    Results in:

    AsOriginalValue             AsDateTime              AsDateTime2_0             AsDate
    --------------------------- ----------------------- ---------------------- ----------
    2018-01-25 14:35:14.3660549 2018-01-25 14:35:14.367 2018-01-25 14:35:14    2018-01-25
    2018-01-25 14:35:14.3665552 2018-01-25 14:35:14.367 2018-01-25 14:35:14    2018-01-25
    2018-01-25 14:35:14.3670555 2018-01-25 14:35:14.367 2018-01-25 14:35:14    2018-01-25
    

    NOTE: beware of rounding.