I want to calculate days to be excluded where the weekday of given date is on Friday, Saturday & Sunday.
I am using below query to calculate days to be excluded based on weekday for a given date.
SELECT 2 * ((DATEPART(WEEKDAY, '2021-10-29') + 5 - 2) / 5) - DATEPART(WEEKDAY, '2021-10-29') / 7
SELECT 2 * ((DATEPART(WEEKDAY, '2021-10-30') + 5 - 2) / 5) - DATEPART(WEEKDAY, '2021-10-30') / 7
SELECT 2 * ((DATEPART(WEEKDAY, '2021-10-31') + 5 - 2) / 5) - DATEPART(WEEKDAY, '2021-10-31') / 7
Actual result should be 2,1,0 respectively for the above. But I am getting 2,3,0.
Can anyone suggest how to get it
Thank you
one way can be to directly check and for Friday, Saturday and Sunday and assign values:
select case when DATEPART(WEEKDAY, '2021-10-29') = 6 then 2
when DATEPART(WEEKDAY, '2021-10-29') = 7 then 1
when DATEPART(WEEKDAY, '2021-10-29') = 1 then 0
else 0 end as days_to_Excluede
but if this is not your requirement then you can wait for some other answers
If I understand correctly, you want to count the days till Sunday? Correct me if I'm wrong. If that's the case I think this will do, otherwise could you explain a bit more:
SELECT 7 - CASE WHEN DATEPART(WEEKDAY, '2021-10-29') = 1 THEN 7 ELSE DATEPART(WEEKDAY, '2021-10-29') - 1 END
SELECT 7 - CASE WHEN DATEPART(WEEKDAY, '2021-10-30') = 1 THEN 7 ELSE DATEPART(WEEKDAY, '2021-10-30') - 1 END
SELECT 7 - CASE WHEN DATEPART(WEEKDAY, '2021-10-31') = 1 THEN 7 ELSE DATEPART(WEEKDAY, '2021-10-31') - 1 END
EDIT:
Since you don't want to use CASE here it is:
SELECT 7 - DATEPART(WEEKDAY, DATEADD(dd, -1,'2021-10-29'))
Related
I am working with a table that has StartDate and EndDate fields. I need to find difference between then in years.
Example:
StartDate = 1/1/2017
EndDate = 12/31/2017
I expect Result = 1 for the date difference.
Also, I'd like to round it to nearest whole number.
Example:
StartDate = 1/1/2017
EndDate = 11/30/2017
I expect Result = 1 for the date difference.
Using datediff function, I am able to get the result, but it isn't rounding to nearest whole number.
Example query:
I am getting 6 years even though 65 months / 12 would be less than 5.5:
select (DATEDIFF(yy, '01/01/2016', '5/31/2021')
+ CASE WHEN abs(DATEPART(day, '01/01/2016') - DATEPART(day, '05/31/2021')) > 15 THEN 1 ELSE 0 END)
select (DATEDIFF(mm, '01/01/2016', '05/31/2021')
+ CASE WHEN abs(DATEPART(day, '01/01/2016') - DATEPART(day, '05/31/2021')) > 15 THEN 1 ELSE 0 END)
DECLARE #startdate DATETIME = '1-1-2017',
#enddate DATETIME = '12-31-2018'
SELECT #startdate as StartDate, #enddate as EndDate,
DATEDIFF(YEAR, #startdate, #enddate)
-
(CASE
WHEN DATEADD(YEAR,
DATEDIFF(YEAR, #startdate,#enddate), #startdate)
> #enddate THEN 1
ELSE 0 END) 'Date difference in Years'
Use this code, I hope it will help you.
So far following query seems to be working okay. My mistake was I dividing by 12 instead of 12.0 for rounding to work correctly. Who knew! :
select
Round((DATEDIFF(mm, '01/01/2016', '07/1/2017')
+ CASE WHEN abs(DATEPART(day, '01/01/2016') - DATEPART(day, '06/30/2017')) > 15 THEN 1 ELSE 0 END) / 12.0, 0)
This may be a bit old but when using Oracle SQL Developer you can use the following. Just add your Dates below. I was using DateTime. This was used to get years between 0 and 10.
TRUNC((MONTHS_BETWEEN(<DATE_ONE>, <DATE_TWO>) * 31) / 365) > 0 and TRUNC((MONTHS_BETWEEN(<DATE_ONE>, <DATE_TWO>) * 31) / 365) < 10
I have table with:
1
2
3
4
5
6
9
10
11
12
and I need to receive:
1-6
9-12
How I can do that?
I need to see that I have two or more range of number i table and that from 1 to 6 and from 9 to 12.
SELECT
CONCAT(MIN(A.b), '-', max(A.b))
FROM
(
SELECT
*,
ROW_NUMBER() OVER (ORDER BY b) RowId
FROM
(VALUES (1), (2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) a(b)
--WHERE
--(a.b >= 1 AND a.b <= 6) OR
--(a.b >= 9 AND a.b <= 12)
) A
GROUP BY
A.b - A.RowId
Trying to run the same view with different date filters depending on which day I'm running it. If I run the query on a Fri, Sat, Sun, or Mon then the dates should be filtered on Tues-Thurs. If I run the query on Tues, Wed, Thurs then the dates should be filtered on Fri-Mon. Those are the only two scenarios. I'm using PostgreSQL.
The view itself is pretty simple otherwise:
select * from tbl_1
where date between ____ and _____
I do have a dates table that I can join to. I've tried something like:
(select date
,case when day_of_week_iso IN(2, 3, 4) THEN 1 ElSE 4 END as "day"
from tbl.date
where date = current_date
) dates
Use extract(dow from current_date) which gives current day of week (Sunday is 0), see Date/Time Functions and Operators.
select *
from tbl_1
where case extract(dow from current_date)
when 2 then date between current_date- 4 and current_date- 1
when 3 then date between current_date- 5 and current_date- 2
when 4 then date between current_date- 6 and current_date- 3
when 5 then date between current_date- 3 and current_date- 1
when 6 then date between current_date- 4 and current_date- 2
when 0 then date between current_date- 5 and current_date- 3
when 1 then date between current_date- 6 and current_date- 4
end;
You could use your own function(s) to shorten the code, but the use of case with all cases is simpler and probably faster.
So this is my table in database:
Worker X have this work result BETWEEN '2015-06-01' AND '2015-06-06':
What I want to do is to count the number of work days but my condition is that if (nb_heures + nb_heures_s) > 4 I count it 1 day but if (nb_heures + nb_heures_s) <= 4 I count it 0.5 day.
So the result I must get from this table 5.5 work days and not 6.
I tried this query but it's not working well:
SELECT
count(CASE WHEN (nb_heures + nb_heures_s) > 4 THEN 1 END) as full_day_work,
count(CASE WHEN (nb_heures + nb_heures_s) <= 4 THEN 0.5 END) as half_day_work
FROM pointage_full pf
WHERE date_pointage BETWEEN '2015-06-01' AND '2015-06-06'
AND pf.id_salarie = 5
How can I reach my objectif ?
COUNT(expr) always returns a bigint, as it simply returns the number of rows for which expr is not NULL.
You can use SUM instead :
SELECT SUM(CASE WHEN (nb_heures + nb_heures_s) > 4 THEN 1 ELSE 0.5 END) as number_of_days
FROM pointage_full pf
WHERE date_pointage BETWEEN '2015-06-01' AND '2015-06-06';
I have a query that produces a random departure dates from 1 to 28 days after the arrival date field:
--Query--
SELECT ArrivalDate, DATEADD(day, 1 + RAND(checksum(NEWID()))
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay
However when I run the Update query the randomisng reduced down to 1 or 2 days, can anyone advise why this is?
--Update Statement--
USE Occupancy
Update B
Set DepartureDate = DATEADD(day, 1 + RAND(checksum(NEWID()))*1.5 * L.LengthofStay, B.ArrivalDate)
FROM LengthOfStay L, Bookings B
Thanks
Wayne
I used the solution below:
UPDATE BOOKINGS
SET DepartureDate =
DATEADD(day,
CASE WHEN Rand(CHECKSUM(NEWID())) BETWEEN 0 and 0.3 THEN 2 ELSE
CASE WHEN Rand(CHECKSUM(NEWID())) BETWEEN 0.3 and 0.5 THEN 3 ELSE
Round(Rand(CHECKSUM(NEWID())) * 28,0) END END,ArrivalDate)