如何使用ON子句从日期时间列中选择日期和时间

时间:2018-10-01 07:43:34

标签: sql sql-server sql-server-2008 sql-server-2005

我有一个联接查询

SELECT  vs.Id as 'VitalId', ms.PatientId as 'PatientId',  ms.CustomerId  as 'CustomerId', ms.CreatedUtc
from A1 a
join B1 b
    on a.CreatedUtc = b.concat(concat(convert(date,S.CreatedUtc),':'),
                               datepart(hour,S.CreatedUtc))

实际上我想将a.CreatedUtc映射到日期和时间到b表(即:-B2)。 这个查询有效吗?因为当我运行此查询时,没有插入记录。

有人可以告诉我如何使用ON子句从Datetime列中仅获取日期和时间吗?

1 个答案:

答案 0 :(得分:1)

这是您想要的吗?

select vs.Id as VitalId, ms.PatientId as PatientId,  
       ms.CustomerId  as CustomerId, ms.CreatedUtc
from A1 a join
     B1 b
     on convert(date, a.CreatedUtc) = convert(date, S.CreatedUtc) and
        datepart(hour, a.CreatedUtc) = datepart(hour, S.CreatedUtc);