SQL SELECT本月的第一天和最后一天。 [英] SQL SELECT First Day and Last Day of the Month.
问题描述
亲爱的专业人员,
我有一个查询来获取本月的第一天和最后一天,但是我的时间戳记存在问题该月的第一天。
I have a query built to get the first and last day of the current month, but I'm having an issue with the time stamp for the First Day of the month.
declare @FirstDOM datetime, @LastDOM datetime
set @FirstDOM = (select dateadd(dd,-(day(getdate())-1),getdate()) )
set @LastDOM = (select dateadd(s,-1,dateadd(mm,datediff(m,0,getdate())+1,0)))
自2015年2月以来,我希望获得以下结果:
Since it's February of 2015, I would like to get results of:
@FirstDOM = 2015-02-01 00:00:00.000
@LastDOM = 2015-02-28 23:59:59.000
@LastDOM是正确的,但是我没有得到时间戳记部分的零@FirstDOM,我得到正确的日期,但是我运行脚本的时间。假设是上午8:50,我会得到:
@LastDOM is correct, but I'm not getting the zeroes for the time stamp portion of @FirstDOM, I'm getting the correct date, but the time of the time I run the script. Say it's 8:50 a.m., I get:
2015-02-01 08:50:49.160
解决此问题的最佳方法是什么?
What is the best way to fix this little snafu?
问候,
尼克
推荐答案
将@FirstDOM转换为 DATE
如下:
Convert @FirstDOM to DATE
as below:
declare @FirstDOM datetime, @LastDOM datetime
set @FirstDOM = (select CONVERT(DATE,dateadd(dd,-(day(getdate())-1),getdate())) )
set @LastDOM = (select dateadd(s,-1,dateadd(mm,datediff(m,0,getdate())+1,0)))
SELECT @FirstDOM,@LastDOM
我希望这会有所帮助!
谢谢,
Swapnil
这篇关于SQL SELECT本月的第一天和最后一天。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!