我必须使用sql server 2012比较日期时间,也就是说,我必须查看mytable1.start_time1的某些日期时间是否在mytable2.start_time2和mytable2.end_time2的范围内。到日期时间我指的是yyyy-mm-dd hh:mm:ss的格式,我可以忽略时间。 我所做的(并且没有成功)如下:
Select CUSTOMER_IDENTITY_NUMBER, a.CREATE_DATE, b.CREATE_DATE, b.END_DATE
from [mytable1]a
join [mytable2]b
on a.CUSTOMER_FK=b.CUSTOMER_PK
where cast(a.CREATE_DATE as date) not between cast(b.CREATE_DATE as date) and cast(b.END_DATE as date)
and cast(a.CREATE_DATE as date)<>cast(b.CREATE_DATE as date)
and cast(a.CREATE_DATE as date)<>cast(b.END_DATE as date)
嗯,它不起作用 - 返回的a.create_date确实落在其他两个日期之间。有没有人有想法? 非常感谢!
答案 0 :(得分:0)
尝试使用略有不同的条件。我不太了解 你使用的逻辑条件。好像你弄错了。
select CUSTOMER_IDENTITY_NUMBER, a.CREATE_DATE, b.CREATE_DATE, b.END_DATE
from [mytable1]a
join [mytable2]b
on a.CUSTOMER_FK=b.CUSTOMER_PK
where
cast(a.CREATE_DATE as date) >= cast(b.CREATE_DATE as date)
and
cast(a.CREATE_DATE as date) <= cast(b.END_DATE as date)