问题
DECLARE @MinutesToAdd int = 20;
DECLARE @StartTimeDate datetime = '2017-06-05 14:37:56.113';
DATEADD(minute,@MinutesToAdd,@StartTimeDate);
Code above adds 20 minutes to StartTimeDate. Is there a good way to remove those 20 minutes not add? Tried to find a solution, but didn't catch one. Any ideas?
回答1:
You don't have any specific function to subtract any dateparts
Just add negative symbol in front
Select DATEADD(minute,-@MinutesToAdd,@StartTimeDate);
or multiply with -1
Select DATEADD(minute,@MinutesToAdd * -1 ,@StartTimeDate);
回答2:
Try this:
DECLARE @MinutesToAdd int = -20;
DECLARE @StartTimeDate datetime = '2017-06-05 14:37:56.113';
select DATEADD(minute,@MinutesToAdd,@StartTimeDate);
来源:https://stackoverflow.com/questions/44582802/remove-minutes-from-datetime-sql