swathi swathi - 3 months ago 19
SQL Question

Date calculation subtracting date

I have a field with sales_start_date( values like 2014-06-17 ,2015-07-23...)
I need to do calculation based on sales_start date..

I have to take the most recent month-end date(i have to force this value to 2016-07-31) and subtract the sale_start_date to get an integer count of the days elapsed. If number of days passed is less than 2 default the value 2

sales_start_date

2016-01-01
2016-07-30


Output

Calculated field
155
2


Can any one help me in writing case statement.
Your help is much appreciated.

Thank You,
Swathi.

Answer

You basically need to use DATEDIFF

SELECT  DATEDIFF(day,<enter start date column here>,<most recent month-end date column here>) AS [Calculated field]
FROM <your table here>
Comments