Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I calculate there's how many weekend days between two dates in SQL Server?

I'm calculating income of hiring equipments for a report. In that, hiring cost in weekend days will be plus 10% more if compare with normal days. So how can I calculate there's how many weekend days between two dates. And in report query, I can't use DECLARE also. Can someone help me to do this. Thank you so much

like image 293
user1789721 Avatar asked Nov 04 '12 18:11

user1789721


People also ask

How do I find weekends in SQL?

MySQL WEEKDAY() Function The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.

How do I find the number of Sundays between two dates in SQL Server?

Here's one way: SELECT early_date , late_date , ( TRUNC (late_date + 1, 'IW') - TRUNC (early_date, 'IW') ) / 7 AS sundays FROM table_x ; This does not depend on your NLS settings.


1 Answers

This should work:

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2012/11/01'
SET @EndDate = '2012/11/05'


SELECT
  (DATEDIFF(wk, @StartDate, @EndDate) * 2)
   +(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday'   THEN 1 ELSE 0 END)
   +(CASE WHEN DATENAME(dw, @EndDate)   = 'Saturday' THEN 1 ELSE 0 END)

http://sqlfiddle.com/#!3/d41d8/5707/0

like image 116
Tim Schmelter Avatar answered Sep 18 '22 13:09

Tim Schmelter