简体   繁体   English

SQL本月1日到月底

[英]SQL 1st of the month to the end of the month

I have an @StartDate and @EndDate. 我有一个@StartDate和@EndDate。

I need the @StartDate to be the day the query is ran(which will always be the first of the month) and the @EndDate to be exaclty at the end of the month no matter if the month is 30 or 31 days, etc. 我需要@StartDate作为查询运行的那天(这将始终是本月的第一天)和@EndDate在月末是exaclty,无论月份是30天还是31天等。

A worked example: 一个有效的例子:

DECLARE @StartDate DATETIME, @EndDate DATETIME

SET @StartDate = '2010-01-01 00:00:00.000'
SET @EndDate = DATEADD(m, 1, @StartDate)

SELECT @StartDate, @EndDate - 1

Basically you want to take the start date, add one month (that's what the DATEADD is doing), and then deduct one day. 基本上你想要开始日期,加上一个月(这就是DATEADD正在做的事情),然后扣除一天。

The output from that query is: 该查询的输出是:

StartOfMonth            EndOfMonth
----------------------- -----------------------
2010-01-01 00:00:00.000 2010-01-31 00:00:00.000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM