ваш первый фрагмент кода является «настилом» даты-времени месяца (делая дату 1-й числа данного месяца без времени), «-1» делает его предыдущим месяцем. Все лишние лишние скобки в этом фрагменте кода вызывают головную боль, вот эквивалент:
dateadd(month,datediff(month,0,getdate())-1,0)
Вот как можно распределить дату и время по разным единицам:
--Floor a datetime
DECLARE @datetime datetime;
SET @datetime = '2008-09-17 12:56:53.430';
SELECT '0 None', @datetime -- none 2008-09-17 12:56:53.430
UNION SELECT '1 Second',DATEADD(second,DATEDIFF(second,'2000-01-01',@datetime),'2000-01-01') -- Second: 2008-09-17 12:56:53.000
UNION SELECT '2 Minute',DATEADD(minute,DATEDIFF(minute,0,@datetime),0) -- Minute: 2008-09-17 12:56:00.000
UNION SELECT '3 Hour', DATEADD(hour,DATEDIFF(hour,0,@datetime),0) -- Hour: 2008-09-17 12:00:00.000
UNION SELECT '4 Day', DATEADD(day,DATEDIFF(day,0,@datetime),0) -- Day: 2008-09-17 00:00:00.000
UNION SELECT '5 Month', DATEADD(month,DATEDIFF(month,0,@datetime),0) -- Month: 2008-09-01 00:00:00.000
UNION SELECT '6 Year', DATEADD(year,DATEDIFF(year,0,@datetime),0) -- Year: 2008-01-01 00:00:00.000
ORDER BY 1
PRINT' '
PRINT 'Note that when you are flooring by the second, you will often get an arithmetic overflow if you use 0. So pick a known value that is guaranteed to be lower than the datetime you are attempting to floor'
PRINT 'this always uses a date less than the given date, so there will be no arithmetic overflow'
SELECT '1 Second',DATEADD(second,DATEDIFF(second,DATEADD(day,DATEDIFF(day,0,@datetime),0)-1,@datetime),DATEADD(day,DATEDIFF(day,0,@datetime),0)-1) -- Second: 2008-09-17 12:56:53.000
второй фрагмент кода не будет правильно указывать дату и время месяца, он будет только перемещать дату на предыдущий месяц и использовать тот же день и время, что и данное время даты.
Кроме того, я не уверен, что вы на самом деле спрашиваете.
Вот разбивка того, что происходит в первом фрагменте кода OP:
select convert(datetime,0),'first datetime "0"'
select datediff(month,0,getdate()), 'months difference between the first datetime (1900-01-01) and given "getdate()"'
select datediff(month,0,getdate())-1, 'months difference between the first datetime (1900-01-01) and month previous to given "getdate()"'
select dateadd(month,datediff(month,0,getdate())-1,0), 'takes the first datetime (1900-01-01) and adds 1328 months onto that'
ВЫВОД:
----------------------- ------------------
1900-01-01 00:00:00.000 first datetime "0"
----------- --------------------------------------------------------------------------------
1329 months difference between the first datetime (1900-01-01) and given "getdate()"
----------- --------------------------------------------------------------------------------------------------
1328 months difference between the first datetime (1900-01-01) and month previous to given "getdate()"
----------------------- --------------------------------------------------------------------
2010-09-01 00:00:00.000 takes the first datetime (1900-01-01) and adds 1328 months onto that