Fatin RIMAWI
Fatin RIMAWI

Reputation: 1

How to make the result in date format using datediff btween 2 columns

I am trying to make the result for datediff(interval result) in date format.

I have a view that had columns with numbers, and the other view contains the today date value.

I want to make a date diff between these 2 values, and get the results as a date too. For example: result 10 for 2018/12/31 and 2018/12/21.

The date format is: YYYY,MM,DD

enter image description here

Upvotes: 0

Views: 201

Answers (2)

Suraj Kumar
Suraj Kumar

Reputation: 5653

If you want to add number of date to given date then you can use the below query.

SELECT  CONVERT(varchar(10), DATEADD(DD, 10, '2018-12-21'), 23) AS DateAfterAdd;

--or

SELECT Cast(CONVERT(varchar(10), DATEADD(DD, 10, '2018-12-21'), 23) as Date) AS DateAfterAdd;

The output is as shown below

DateAfterAdd
2018-12-31

Upvotes: 0

Yakov R.
Yakov R.

Reputation: 624

If I understand correctly you want to subtract x days from a given date. Use the DATEADD function.

SELECT 10 F1, '2018-12-31' Date2, DATEADD(DAY, -10, GETDATE())F3

Notice: In order to subtract using the DATEADD function we use a negative value (-10)

enter image description here

Upvotes: 4

Related Questions