SQL中的Between子句与两个日期中的Month和year。 [英] Between clause in SQL with Month and year in two dates.

查看:904
本文介绍了SQL中的Between子句与两个日期中的Month和year。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是为了知识:

我想用日期过滤我的查询。从日期和迄今为止,但我想根据两个日期的月份和年份进行过滤。

例如

 选择 * 来自 #Table 其中​​ myDate 介于之间(月(FromDate) AND 年(FromDate)) AND (月(ToDate) AND 年(Todate))





应该是什么确切的语法?

如果问题不清楚请告诉我

解决方案

不是那样的!



相反,从所有三个日期中删除日期部分,并使用它们。

 DATEADD(月,DATEDIFF(月, 0 ,MyDateColumnOrValue), 0 

将日期转换为该月的第1天,所以你可能想要这样的东西:

< pre lang =SQL> ... WHERE DATEADD(月,DATEDIFF(月, 0 ,myDate ), 0 BETWEEN
DATEADD(月,DATEDIFF(月, 0 ,FromDate), 0 AND
DATEADD (月,DATEDIFF(月, 0 ,ToDate), 0


您好,



试试这个...





 中选择 *  #Table 
其中
MONTH(myDate) BETWEEN MONTH(FromDate) AND MONTH(ToDate)
AND
年(myDate)之间 Y. EAR(FromDate) AND 年(ToDate)







我没有检查过,所以如果没有得到理想的输出你就可以回来。





希望这会有所帮助你。





干杯


Just for knowledge:
I want to filter my query with dates. from Date and To date but i want to filter according to Month and year of both dates.
e.g

Select * from #Table where myDate between (Month(FromDate) AND Year(FromDate)) AND (Month(ToDate) AND Year(Todate))



What should be the exact syntax for this?
Let me know if question isnt clear

解决方案

Not like that!

Instead, remove the day part from all three dates, and use them.

DATEADD(month, DATEDIFF(month, 0, MyDateColumnOrValue), 0)

Will convert a date to the 1st of the month, so you might want something like:

... WHERE DATEADD(month, DATEDIFF(month, 0, myDate), 0) BETWEEN 
          DATEADD(month, DATEDIFF(month, 0, FromDate), 0) AND
          DATEADD(month, DATEDIFF(month, 0, ToDate), 0)


Hi,

Try this...


Select * from #Table 
where 
MONTH(myDate ) BETWEEN MONTH(FromDate) AND MONTH(ToDate)
AND 
YEAR(myDate ) BETWEEN YEAR(FromDate) AND YEAR(ToDate)




I have not checked it, so you can be back if did not get desired output.


Hope this will help you.


Cheers


这篇关于SQL中的Between子句与两个日期中的Month和year。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆