[英]Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP
I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.我知道这很容易,但在比较 SQL 中的日期时我总是做噩梦,请有人帮我解决这个问题,谢谢。
I need to get the month and year of now then compare it to a date stored in a DB.我需要获取现在的月份和年份,然后将其与存储在数据库中的日期进行比较。
Time Format in the DB:数据库中的时间格式:
2015-08-17 11:10:14.000 2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count.我需要将月份和年份与现在进行比较,如果它大于 12 个月,我将增加一个计数。 I just need the number of rows where this argument is true.
我只需要此参数为真的行数。
I assume you have a datetime field.我假设您有一个日期时间字段。
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.您可以使用DATEDIFF函数,它采用“交叉边界”的类型、开始日期和结束日期。
Your boundary is the month because you are only interested in year and month, not days, so you can use the month
macro.你的边界是月份,因为你只对年和月感兴趣,而不是天,所以你可以使用
month
宏。
Your start time is the value stored in the table's row.您的开始时间是存储在表行中的值。
Your end time is now.你的结束时间是现在。 You can get system time selecting SYSDATETIME function.
您可以选择SYSDATETIME函数来获取系统时间。
So, assuming your table is called mtable
and the datetime object is stored in its date
field, you simply have to query:因此,假设您的表名为
mtable
并且 datetime 对象存储在其date
字段中,您只需查询:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.