列出使用两年的两年记录 [英] Listing Records From Two Years Using Month
问题描述
使用以下日期;
SN名称STARTDATE ENDDATE
1. BABS 01/04/2011 03/06/2012
2. JKK 2011年10月10日23/12/2012
3. HAN 04/04/2011 06/02/2012
4.哈哈2011年5月16日2012年5月5日
假设正在搜索具有05/2011年的月份和年份的记录
使用以下代码;
SELECT * FROM mytable WHERE 05 MONTH(StartDate)和MONTH(EndDate)AND之间
(YEAR(StartDate)= 2011 OR YEAR(EndDate)= 2011)
执行上面显示的记录上方的语句是
SN名称STARTDATE ENDDATE
1. BABS 01/04/2011 03/06/2012
2. JKK 2011年10月10日23/12/2012
4.哈哈2011年5月16日2012年5月5日
没有显示记录3,该记录应该包括在内.查看记录3.month(startdate)小于month(enddate)(04< 02)如何处理起始月小于结束月的记录?
http://www.w3schools.com/sql/sql_between.asp [ ^ ]换句话说,您可以说您想要的日期介于明确地有两个日期.
这只是您可以使用的替代想法"..
从mytable中选择*,其中(DATEDIFF(yy,StartDate,EndDate)* 1000 + DATEDIFF(mm,StartDate,EndDate))> 2004
(日期差异大于2年零4个月)
或
一起获取"YYYYMM"以进行比较
免责声明-这些只是一般性的想法,与技术无关.
您还可以像Visual Studio或LINQPad中那样使用工具来构建SQL请求,然后查看生成的SQL.
blockquote>
Using the date below;
SN NAME STARTDATE ENDDATE
1. BABS 01/04/2011 03/06/2012
2. JKK 10/10/2011 23/12/2012
3. HAN 04/04/2011 06/02/2012
4. LOL 16/05/2011 05/05/2012
Assuming am search for record that has month and year of 05/2011
With the following code;
SELECT * FROM mytable WHERE 05 BETWEEN MONTH(StartDate) AND MONTH(EndDate) AND
(YEAR(StartDate) = 2011 OR YEAR(EndDate) = 2011)
Executing the statement above the record shown is
SN NAME STARTDATE ENDDATE
1. BABS 01/04/2011 03/06/2012
2. JKK 10/10/2011 23/12/2012
4. LOL 16/05/2011 05/05/2012
Record 3 is not showing, which suppose to be included. Looking at Record 3. the month(startdate) is less then the month(enddate) (04<02) How can I handle a record where the starting month is less than the ending month?解决方案http://www.w3schools.com/sql/sql_between.asp[^] In other words, you can say you want the date between two dates explicitly.
This is just alternate "idea" that you could use..
select * from mytable where (DATEDIFF(yy,StartDate,EndDate)*1000 + DATEDIFF(mm,StartDate,EndDate)) > 2004
(Date difference greater than 2 yr and 4 months)
or
get "YYYYMM" together to compare
Disclaimer - these are only general ideas- non technology specific.
You can also uses tool to builds you SQL request like those in Visual Studio or LINQPad and then look at the generated SQL.
这篇关于列出使用两年的两年记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!