简体   繁体   English

现在获取月份和年份,然后计算 SQL/Classic ASP 中超过 12 个月的行数

[英]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.

相关问题 SQL查询可提供过去12个月按周,月,年分组的未清作业数 - SQL Query to provide a count of jobs that were open grouped by week, month, year for the last 12 months 计算一年中每个月的行-SQL Server - Count rows each month of a year - SQL Server 如何获得过去 12 个月的每个月的计数 - How to get count for each month for the last 12 months sql查询获取记录每年12个独特的月份记录,每年设置不同的卷号 - sql query to get records 12 unique months records of every year, every year has set different volume number 如何在SQL中查询当年月份的月份和天数? - How to query months and number of days in a month of current year in SQL? SQL 查询:创建一个表,其中的行按月/年划分,并计算值的数量,其中“01/月/年”介于两个日期列之间 - SQL Query: CREATE a table with rows divided by month/year and COUNT the number of values WHERE '01/month/year' IS BETWEEN two date-columns SQL技巧计数行数按月? - SQL trick count number of rows by months? SQL查询通过输入年份来获取到目前为止的月份列表 - SQL query to get list of months till now by entering year 计算日期时间每个月内的行数,按年份月份分组 - Count number of rows within each month of a datetime, group by year month Oracle SQL:获取最近八个月的数据(有YEAR和MONTH列) - Oracle SQL: Get the last eight months of data (with YEAR and MONTH columns)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM