Hi
How to subtract 2 dates & get No Of Days
Thanks
Hi
How to subtract 2 dates & get No Of Days
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Brahma Prakash ShuklaPosted Jun 19, 2023, 7:34 AM
DECLARE @StartDate DATE = '2023-06-15';
DECLARE @EndDate DATE = '2023-06-19';
DECLARE @NumberOfDays INT;
SET @NumberOfDays = DATEDIFF(day, @StartDate, @EndDate);
SELECT @NumberOfDays;
Vishal YelvePosted Jun 19, 2023, 6:48 AM
Hi Ramco,
Try this
OutPut
Raj BhattPosted Jun 19, 2023, 5:44 AM
In sql you can do by using DATEDIFF()
SELECT DATEDIFF(day, '2023-06-10', '2023-06-15') AS NumberOfDays;
Amit MohantyPosted Jun 19, 2023, 5:23 AM
cjardPosted Jun 19, 2023, 5:12 AM
Like you would numbers
laterDate - earlierDateThe operation results in a TimeSpan object that describes the period of time between the two dates
For example:
daysBetween will be a double, as the period between the dates could have fractions of a day; the time period between noon on 2nd Jan and midnight in 1st Jan is 1.5 days. If you only want whole days, you can cast to an int, use Math.Floor etc.
Take a look at the other properties of a TimeSpan in the documentation for further ideas on how to work with it
Note, like numbers, if you do earlierDate - laterDate, you'll get a negative TimeSpan