我正在尝试执行以下查询以返回一个月中的第一个和最后一个日期,但查询不返回任何值。
DECLARE @today AS DATETIME
SET @today= CAST('2012-08-24' AS DATETIME)
--First day of month
SELECT DATEADD(dd, -DAY(@today) + 1, @today)
UNION
--Last day of month
SELECT DATEADD(dd, -DAY(DATEADD(mm, 1, @today)), DATEADD(mm, 1, @today))
但是,在执行它时,我得到以下结果:
10:36:13 [DECLARE - 1 row(s), 0.005 secs] Command processed
... 1 statement(s) executed, 1 row(s) affected, exec/fetch time: 0.005/0.000 sec [1 successful, 0 warnings, 0 errors]
我想查看查询的结果,而不仅仅是命令已被处理。如何解决这个问题?当使用它而不像这样声明变量时:
--First
SELECT DATEADD(dd, -DAY(CAST('2012-08-24' AS DATETIME)) + 1, CAST('2012-08-24' AS DATETIME))
UNION
--Last day of month
SELECT DATEADD(dd, -DAY(DATEADD(mm, 1, CAST('2012-08-24' AS DATETIME))), DATEADD(mm, 1, CAST('2012-08-24' AS DATETIME)))
它返回预期的结果:
2012-08-01 00:00:00
2012-08-31 00:00:00
如何解决这个问题?