SQL ignores nanoseconds when comparing datetime2

I have 2 tables with a field datetime2each. Nanoseconds are also stored in one of the tables, and only milliseconds in the other (this data cannot be changed). Now I want to compare 2 tables and check if the values ​​match datetime2, but I have to ignore 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

Currently my request is as follows:

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

As you can guess, I am not getting any results for this example, because the nanoseconds are different from each other.

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

Edit:

I know that I must use the correct JOINs. This request was just verified if I made no errors in my request usingJOIN

+4
4

, . ?

:

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;

:

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

. .

+7

CAST DATETIME

select t1.* from Table1 t1, Table12 t2 
where CAST(t1.tradeDateTime as DATETIME) = CAST(t2.tradeDate as DATETIME)

DECLARE @1 DATETIME2='2018-01-24 10:51:23.9976180'
, @2 DATETIME2 = '2018-01-24 10:51:23.9970000'


SELECT @1, @2,  Test =CASE WHEN CAST(@1 AS DATETIME) = CAST(@2 AS
DATETIME) THEN 1 ELSE 0 END
+3

DateTime, . , prober JOINS:

SELECT t1.*
FROM Table1 t1
    JOIN Table12 t2 ON t2.Joinable_ID = t1.Joinable_ID
WHERE CAST(t1.tradeDateTime AS DATETIME) = CAST(t2.tradeDate AS DATETIME)
+2

WHERE . CTE:

 with t1 as 
           (select cast(tradeDateTime as datetime) as tradeDateTimeNoTime
            from Table1)
    , t2 as 
           (select cast(tradeDate as datetime) as tradeDateNoTime 
            from Table2)

 select t1.*
      , t2.* 
 from t1 
 inner join t2 
 on t1.tradeDateTimeNoTime = t2.tradeDateNoTime

Whatever solution you use, I would definitely recommend using explicit syntax JOINrather than comma syntax. Final note: joining in a datetime field is not ideal for a start, so you might consider an approach that uses identifiers in both tables.

+2
source

Source: https://habr.com/ru/post/1692714/


All Articles