How to view the date - 60 days in a specific date format in sql?

I am trying to view a date of up to 60 days of a certain date in this date format: March 03, 2013 .. how can I subtract 60 days from this date ... I used this code, but didn’t work, because it subtracts only from days and does not deduct from the month or year:

Select LTRIM(DAY(@date - 60))+'-'+CONVERT(varchar(3), DATENAME(MONTH,@date))+'-'+LTRIM(YEAR(@date)) 

Any help?

+4
source share
2 answers

try it -

select REPLACE(CONVERT(VARCHAR(11), DATEADD(dd, -60, cast('03-Mar-2013' as date)), 106), ' ', '-') AS [DD-Mon-YYYY]
+2
source

Try

Select DateAdd(DD,-60,cast('03-Mar-2013' as date))

Returns

2013-01-02

To formatt

Select Replace(Convert(varchar(11),DateAdd(DD,-60,cast('03-Mar-2013' as date)),106),' ','-')

Returns

02-Jan-2013
+4
source

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


All Articles