Subtracting from the number of days in SQL Server

I would like to subtract the number of days from the date in the format ( YY-MM-DD HH:MI:SS ). Is there any automatic function to accomplish this or is it going to be comprehensive coding to accomplish this task. I am writing a stored procedure that covers the above task.

To be more clear, let's say that this is the date 2011-12-07 06:05:19.200 , and I want to subtract 19 days from it, and the result will be 2011-11-18 06:05:19.200 , and the number of days will be a parameter in SP

+4
source share
2 answers

You deleted your identical question, which I answered earlier, but here is a function that does what you need.

 CREATE FUNCTION SUBTRACT_DAYS( @date AS DATETIME, @days AS INT ) RETURNS DATETIME BEGIN RETURN DATEADD(dd, -@days , @date); END; SELECT dbo.SUBTRACT_DAYS('2011-12-07 06:05:19.200', 19); > 2011-11-18 06:05:19.200 
+8
source

How about this:

 CREATE PROCEDURE dbo.DoSomethingUseful @InputDate DATETIME, @NumberOfDays INT AS BEGIN SELECT DATEADD(Day, @NumberOfDays, @InputDate) END 

and then call it like this:

 EXEC dbo.DoSomethingUseful @InputDate = '2011-12-07T06:05:19.200', @NumberOfDays = -19 

returns:

 2011-11-18 06:05:19.200 
+4
source

Source: https://habr.com/ru/post/1400568/


All Articles