SQL Server - Get Last Business Data excluding holidays and Weekends - sql-server

I use this query to get the last business date excluding weekends:
DECLARE #LastBizDate as DATETIME
SELECT #LastBizDate = (DATEADD(DAY, CASE DATENAME(WEEKDAY, GETDATE())
WHEN 'Saturday' THEN -1
WHEN 'Sunday' THEN -2
ELSE -1 END, DATEDIFF(DAY, 0, GETDATE())))
SELECT #LastBizDate AS 'Last_Business_Date'
But how can I exclude the holidays?
I have this tbl_Holidays table with sample data:
-------------------------
|Holiday_Date |
|-----------------------|
|2017-04-14 00:00:00.000|
|2017-05-01 00:00:00.000|
|2017-10-18 00:00:00.000|
|2017-12-25 00:00:00.000|
-------------------------
Note: no need to consider that when the holiday falls on Sunday, then Monday will be considered Off. Again, no need to consider that.
Thank you in advance.

I think you need a loop to do this.
After excluding saturday and sunday, you need to check whether the date is in the holiday table. Again you need to check the date (calculated after excluding from holiday table )falls on sunday and saturday.
DECLARE #last_wrk_day DATETIME
,#today DATETIME
DECLARE #TblHoliday AS TABLE (Holiday_Date DATE)
INSERT INTO #TblHoliday
VALUES ('2017-04-14 00:00:00.000')
,('2017-05-01 00:00:00.000')
,('2017-10-18 00:00:00.000')
,('2017-12-25 00:00:00.000')
,('2017-04-13 00:00:00.000')
SET #today = '2017-04-10 00:00:00.000' --getdate()
SET #last_wrk_day = dateadd(day, - 1, #today)
WHILE (
(
SELECT TOP 1 1
FROM #TblHoliday
WHERE Holiday_Date = #last_wrk_day
) = 1
OR DATEPART(dw, #last_wrk_day) IN (1, 7)
)
BEGIN
SET #last_wrk_day = dateadd(day, - 1, #last_wrk_day)
END
SELECT #last_wrk_day
OUTPUT
2017-04-13 00:00:00.000

Related

How to get date from day name?

How can I get the date of specific day ? Like if I have Thursday or month number ?
If I give 12 for instance I want to get the date of 12th day of this month. Or if I give 'Sun' or 'Sat' is it possible to get the dates of these days ?
DATEFROMPARTS function can construct a date from day, month and year.
DATEPARTS does the opposite - gives you the day, month, year, hour, etc. of a date. Or you can use functions like YEAR, MONTH and DAY.
You can deconstruct the value returned by GETDATE function and construct whatever date you want. Here is for example how to get the date for 12th day of the current month:
select DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 12)
Converting 'Sun' or 'Sat' to date is a bit more difficult. First, they aren't quite deterministic. If today is Friday, "Sunday this week" means "next Sunday" in some parts of the world and "last Sunday" in others. You should implement your own logic based on the value returned by DATEPART(dw, GETDATE()) (which will give you the day of the week).
To find the weekday of the current month
DECLARE #daynumber INT = 12
SELECT datename(weekday, dateadd(d, #daynumber - 1, getdate()))
To find the dates of the current month of a given weekday
DECLARE #dayname char(3) = 'sat'
;WITH CTE as
(
SELECt TOP
(datediff(D, eomonth(getdate(), -1),eomonth(getdate())))
dateadd(d,row_number()over(ORDER BY 1/0),
eomonth(getdate(),-1))date
FROM
(values(1),(2),(3),(4),(5),(6))x(x),
(values(1),(2),(3),(4),(5),(6))y(x)
)
SELECT day(date) monthday, date
FROM CTE
WHERE left(datename(weekday, date),3) = #dayname
select sysdatetime(); --2018-12-13 16:29:56.0560574
---If I give 12 for instance I want to get the date of 12th day of this month.
declare #numDate int = 12;
select dateadd(m, datediff(m,0,getdate()),#numDate - 1 ); --2018-12-12 00:00:00.000
--Or if I give 'Sun' or 'Sat' is it possible to get the dates of these days ?
declare #text nvarchar(20) = 'Sunday';
declare #dateStart date = dateadd(month, datediff(month, 0, sysdatetime()), 0),
#days int =( select (DAY(dateadd(dd,-1,DATEADD(m,1,cast(2018 as varchar(4)) + '-' + cast(12 as varchar(2)) +'-01')))));
declare #dateEnd date = DATEADD(day,#days-1,#dateStart);
;WITH CTE (Dates,EndDate) AS
(
SELECT #dateStart AS Dates,#dateEnd AS EndDate
UNION ALL
SELECT DATEADD(day,1,Dates),EndDate
FROM CTE
WHERE DATEADD(day,1,Dates) <= EndDate
)
SELECT CTE.Dates, DATENAME(DW, CTE.Dates)
FROM CTE
where DATENAME(DW, CTE.Dates) = #text;
Result:
Dates,Day
2018/12/2,Sunday
2018/12/9,Sunday
2018/12/16,Sunday
2018/12/23,Sunday
2018/12/30,Sunday
-- Here is how to get week day name to week day number
DECLARE #T TABLE (Dow INT, NameOfDay VARCHAR(15), ShortName CHAR(3));
WITH Days AS
(
SELECT TOP 7
ROW_NUMBER() OVER(PARTITION BY object_id ORDER BY object_id) AS RowNo
FROM
sys.all_columns
)
INSERT INTO #T
SELECT
RowNo,
DATENAME(WEEKDAY, RowNo - 1),
LEFT(DATENAME(WEEKDAY, RowNo - 1), 3)
FROM
Days
SELECT
*
FROM
#T;
-- Here is how to get start of period
SELECT
DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) AS StartOfDay,
DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0) AS StartOfWeek,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS StartOfMonth,
DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) AS StartOfYear;
-- An example
WITH
StartPeriods AS
(
SELECT DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0) AS StartOfWeek
),
SelectedDay AS
(
SELECT
Dow - 1 AS Dow,
(SELECT StartOfWeek FROM StartPeriods) AS StartOfWeek
FROM
#T
WHERE
ShortName = 'Wed'
)
SELECT
DATEADD(DAY, Dow, StartOfWeek)
FROM
SelectedDay;

Script to filter by most recent end of week date

My company's reporting week is Monday through Sunday. On Monday morning's I run several queries using Microsoft SQL Server Management Studio to report on business activity for the previous week. I currently use declaration statements to pull the desired date range. This works quite well as long as I'm running reports on Monday. However, if Monday was a holiday and I'm not running reports until Tuesday, I need to manually modify my date range for each query. How can I modify my date filter to retrieve records through the previous "Sunday" so it doesn't matter what day I actually run the report.
Here's a sample query;
Declare #DATEFROM SMALLDATETIME = (CONVERT(datetime, getdate() + cast('00:00' as datetime)) - 8), #DATETO smalldatetime = (CONVERT(datetime, getdate() + cast('23:59' as datetime))-2);
Create Table #SALES ([PartNumber] CHAR(5), [DateSold] SMALLDATETIME)
Insert Into #SALES Select '10190', '6/3/2018 11:00'
Insert Into #SALES Select '10213', '6/8/2018 8:00:00 AM'
Insert Into #SALES Select '10214', '6/5/2018 9:30:00 AM'
Insert Into #SALES Select '10215', '6/4/2018 1:00:00 PM'
Insert Into #SALES Select '10217', '6/6/2018 1:00:00 PM'
Insert Into #SALES Select '10219', '6/7/2018 12:00:00 PM'
Insert Into #SALES Select '10220', '6/9/2018 3:30:00 PM'
Insert Into #SALES Select '10221', '6/11/2018 8:30:00 AM'
Insert Into #SALES Select '10222', '6/11/2018 2:30:00 PM'
Insert Into #SALES Select '10225', '6/8/2018 8:00:00 AM'
Insert Into #SALES Select '10227', '6/10/2018 9:00:00 AM'
Insert Into #SALES Select '10231', '6/10/2018 1:00:00 PM'
Insert Into #SALES Select '10233', '6/2/2018 8:00:00 AM';
SELECT S.PartNumber, S.DateSold
FROM #SALES S
WHERE DATESOLD BETWEEN #DATEFROM AND #DATETO
ORDER BY DateSold;
DROP TABLE #SALES
First, a few items of interest:
SQL Server's SET DATEFIRST command allows you to choose any weekday as the first day of the week, from 1 (Monday) through 7 (Sunday).
You can query the current value of this setting using the expression ##datefirst.
DATEPART(weekday, getdate()) will return a number for the current weekday where 1 indicates the day set by SET DATEFIRST, 2 indicates the day after, etc.
So suppose I want to answer the question: how many days prior to some arbitrary date #TestDate was the most recent Monday? The number of days that have passed since the most recent beginning of a week is DATEPART(weekday, #TestDate) - 1, and the number of days that pass between a Monday and the beginning of a week is ##datefirst - 1, so the number of days that have passed since the most recent Monday is the sum of those quantities modulo 7:
declare #TestDate date = convert(date, getdate());
declare #DaysPastMonday int = (##datefirst + datepart(weekday, #TestDate) - 2) % 7;
With this information you can get your date range very easily:
declare #DateTo date = dateadd(day, -#DaysPastMonday, #TestDate);
declare #DateFrom date = dateadd(day, -7, #DateTo);
But note that both of the dates I've selected here are Mondays, whereas you want a Monday through a Sunday. The reason I've done it this way is that if you're going to be looking at fields that potentially have a time component as well as a date (e.g., datetime or datetime2), and you want to use BETWEEN, then you need to make sure that your end date has the latest time of day that SQL Server is capable of representing. I find it cleaner to use a date range that's inclusive on the start date and exclusive on the end date. So in place of an expression like this:
x BETWEEN #DateFrom AND #DateTo
You'd write your queries to use an expression like this:
x >= #DateFrom AND x < #DateTo
Declare #DATEFROM datetime = DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0),
#DATETO datetime = DATEADD(ms, -3, (select DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 7)));

How to calculate Saturday in between two dates in SQL Server

Week ends on Saturday and new week starts on Sunday.
I would like to determine date which will be a Saturday and split dates in SQL Server. Example below:
Start_date - 09/11/2018 - Friday
End_date - 12/11/2018 - Monday
Total number of days = 4
I would like split the days and end results as
Start_date - 09/11/2018 - Friday
Date1 - 10/11/2018 - Saturday
Total number of days = 2
Date2 - 11/11/2018 - Sunday
End_date - 12/11/2018 - Monday
Total number of days = 2
Another example ( start_date and end_date if there are more number of Saturdays e.g. )
start-date - 04/05/2017
end_date - 31/05/2017
Then results should be like below :-
Date1 Date2 no. of days.
------------------------------------
04/05/2017 06/05/2017 3
07/05/2017 13/05/2017 7
14/05/2017 20/05/2017 7
21/05/2017 27/05/2017 7
28/05/2017 31/05/2017 4
Please help.
Thanks & Regards,
VG
If I am not mistaken you are looking for something like this:
DECLARE #StartDate DATE = '2017-05-04'
DECLARE #EndDate DATE = '2017-05-31'
DECLARE #OutputTABLE AS TABLE
(
StartDate DATE NOT NULL,
EndDate DATE NOT NULL
)
DECLARE #NumberOfWeeks INT = DATEDIFF(week, #StartDate, #EndDate)
DECLARE #Counter INT = 0
DECLARE #TempDate DATE = DATEADD(week, #Counter, #StartDate)
WHILE #NumberOfWeeks >= 0
BEGIN
IF #NumberOfWeeks = 0
BEGIN
INSERT INTO #OutputTABLE VALUES (#TempDate, #EndDate);
END
ELSE
BEGIN
INSERT INTO #OutputTABLE VALUES (#TempDate, DATEADD(DAY, -1, DATEADD(week, DATEDIFF(week ,0 , #TempDate) + 1, -1)));
END
SET #TempDate = DATEADD(week, #Counter + 1, DATEADD(day, -1, DATEADD(week, DATEDIFF(week, 0, #StartDate), 0)))
SET #NumberOfWeeks = #NumberOfWeeks - 1
SET #Counter = #Counter + 1
END
SELECT StartDate,
EndDate,
DATEDIFF(day, StartDate, EndDate) + 1 AS NumberOfDays
FROM #OutputTABLE
Here is one way. I couldn't figure out a way with a recursive CTE since you can't aggregate in the recursive part of a recursive CTE.
DECLARE #MinDate DATE = '20170504',
#MaxDate DATE = '20170531'
DECLARE #StartDate datetime, #EndDate datetime
--DATE TABLE... ONE ROW FOR EVERY DAY IN RANGE
IF OBJECT_ID('tempdb..#DateTable') IS NOT NULL DROP TABLE #DateTable
SELECT TOP (DATEDIFF(DAY, #MinDate, #MaxDate) + 1)
Dates = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, #MinDate)
INTO #DateTable
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
CREATE TABLE #Results (Date1 DATETIME, Date2 DATETIME, NumOfDays INT)
--INSERT FIRST ROW IN CASE IT STARTS ON A DAY OTHER THAN SUNDAY
INSERT INTO #Results
SELECT
MIN(Dates) as Date1
,MIN(CASE WHEN DATEPART(WEEKDAY,Dates) = 7 THEN Dates END) as Date2
,DATEDIFF(DAY,MIN(Dates),MIN(CASE WHEN DATEPART(WEEKDAY,Dates) = 7 THEN Dates END)) + 1 as NoOfDays
FROM #DateTable
SET #StartDate = (SELECT MIN(CASE WHEN DATEPART(WEEKDAY,Dates) = 1 THEN Dates END) FROM #DateTable)
SET #EndDate = (SELECT MAX(CASE WHEN DATEPART(WEEKDAY,Dates) = 1 THEN Dates END) FROM #DateTable)
--INSERT ALL FULL WEEKS
WHILE #StartDate < #EndDate
BEGIN
INSERT INTO #Results
SELECT
MIN(Dates) as Date1
,MIN(CASE WHEN DATEPART(WEEKDAY,Dates) = 7 THEN Dates END) as Date2
,DATEDIFF(DAY,MIN(Dates),MIN(CASE WHEN DATEPART(WEEKDAY,Dates) = 7 THEN Dates END)) + 1 as NoOfDays
FROM #DateTable
WHERE Dates >= #StartDate
SET #StartDate = DATEADD(DAY,7,#StartDate)
END
--INSERT LAST ROW IF IT ISN'T A FULL WEEK
IF (SELECT MAX(Date2) FROM #Results) <> #MaxDate
BEGIN
INSERT INTO #Results
SELECT
MIN(Dates) as Date1
,MAX(Dates) as Date2
,DATEDIFF(DAY,MIN(Dates),MAX(Dates)) + 1 as NoOfDays
FROM #DateTable
WHERE Dates > (SELECT MAX(Date2) FROM #Results)
END
SELECT * FROM #Results
DROP TABLE #Results
DROP TABLE #DateTable
RETURNS
+-------------------------+-------------------------+-----------+
| Date1 | Date2 | NumOfDays |
+-------------------------+-------------------------+-----------+
| 2017-05-04 00:00:00.000 | 2017-05-06 00:00:00.000 | 3 |
| 2017-05-07 00:00:00.000 | 2017-05-13 00:00:00.000 | 7 |
| 2017-05-14 00:00:00.000 | 2017-05-20 00:00:00.000 | 7 |
| 2017-05-21 00:00:00.000 | 2017-05-27 00:00:00.000 | 7 |
| 2017-05-28 00:00:00.000 | 2017-05-31 00:00:00.000 | 4 |
+-------------------------+-------------------------+-----------+
This will compute your Date2 from your Date1
print dateadd(day,-1,dateadd(wk,datepart(wk,'4/5/2017'),'1/1/2017'))

create a resultset of calendar dates

I should use a stored procedure to fill a calendar for 10 years in a SQL database!
Three columns should I fill in with the records: date,weekday or weekend, dayname( monday,...).
Can somebody help me?
You can do it dynamically like this:
WITH Dates AS (
SELECT CONVERT(DATE, getdate()) as [Date]
UNION ALL
SELECT DATEADD(DAY, 1, [Date])
FROM Dates
where Date < dateadd(yy, 10, getdate())
)
SELECT [Date]
FROM Dates
OPTION (MAXRECURSION 4000)
try this version (floors the datetime and adds all requested columns):
;WITH Dates AS (
SELECT DATEADD(day,DATEDIFF(day,0,GETDATE()),0) as DateOf,
CASE WHEN datename(weekday,getdate()) IN ('Saturday','Sunday') THEN 'Weekend'
ELSE 'WeekDay'
END DayType,
datename(weekday,getdate()) DayOfWeekName
UNION ALL
SELECT DateOf+1,
CASE WHEN datename(weekday,DateOf+1) IN ('Saturday','Sunday') THEN 'Weekend'
ELSE 'WeekDay'
END DayType,
datename(weekday,DateOf+1) DayOfWeekName
FROM Dates
where DateOf < dateadd(yy, 10, getdate())
)
SELECT DateOf,DayType,DayOfWeekName
FROM Dates
OPTION (MAXRECURSION 4000)
to insert into a table try this:
DECLARE #DateTable table (DateOf datetime, DayType char(7), DayOfWeekName varchar(10))
;WITH Dates AS (
SELECT DATEADD(day,DATEDIFF(day,0,GETDATE()),0) as DateOf,
CASE WHEN datename(weekday,getdate()) IN ('Saturday','Sunday') THEN 'Weekend'
ELSE 'WeekDay'
END DayType,
datename(weekday,getdate()) DayOfWeekName
UNION ALL
SELECT DateOf+1,
CASE WHEN datename(weekday,DateOf+1) IN ('Saturday','Sunday') THEN 'Weekend'
ELSE 'WeekDay'
END DayType,
datename(weekday,DateOf+1) DayOfWeekName
FROM Dates
where DateOf < dateadd(yy, 10, getdate())
)
INSERT INTO #DateTable (DateOf,DayType,DayOfWeekName)
SELECT DateOf,DayType,DayOfWeekName
FROM Dates
OPTION (MAXRECURSION 4000)
select top 10 * from #DateTable
OTUPUT:
DateOf DayType DayOfWeekName
----------------------- ------- -------------
2011-05-16 00:00:00.000 WeekDay Monday
2011-05-17 00:00:00.000 WeekDay Tuesday
2011-05-18 00:00:00.000 WeekDay Wednesday
2011-05-19 00:00:00.000 WeekDay Thursday
2011-05-20 00:00:00.000 WeekDay Friday
2011-05-21 00:00:00.000 Weekend Saturday
2011-05-22 00:00:00.000 Weekend Sunday
2011-05-23 00:00:00.000 WeekDay Monday
2011-05-24 00:00:00.000 WeekDay Tuesday
2011-05-25 00:00:00.000 WeekDay Wednesday
(10 row(s) affected)
I apporached this as a tally table problem. I am using spt_values from Master as my tally table. It only goes up to 2048 which is enough data for 5.5 years. You can create your own tally table with as many numbers as you need.
Declare #startDate Date = '1/1/2011';
SELECT DateAdd(d, number, #startDate) [Date],
CASE WHEN DATEPART(dw, DateAdd(d, number, #startDate)) IN (1,7) THEN 'Weekend' ELSE 'Weekday' END [WeekDayEnd],
DateName(weekday, DateAdd(d, number, #startDate)) DayOfWeek
FROM spt_values
WHERE type = 'P';
This gets the following results:
Date WeekDayEnd DayOfWeek
2011-01-01 Weekend Saturday
2011-01-02 Weekend Sunday
2011-01-03 Weekday Monday
2011-01-04 Weekday Tuesday
2011-01-05 Weekday Wednesday
2011-01-06 Weekday Thursday
2011-01-07 Weekday Friday
2011-01-08 Weekend Saturday
2011-01-09 Weekend Sunday
2011-01-10 Weekday Monday
USE THIS
set nocount on
SET DATEFIRST 7;
go
select date,
datename(dw,datepart(dw,date)) Day,
datepart(dw,date) Day,
'Segment' = case
when datepart(dw,date)in (5,6) then 'WEEK_END' else 'Week_day' end
from calenderdate
set nocount off
> set nocount on
>
> select date,
> substring(cast(datename(dw,datepart(dw,date))as
> varchar(10)),1,3) Day, 'Segment' =
> case when datepart(dw,date)in (5,6)
> then 'WEEK_END' else 'Week_day' end
> from calenderdate
>
> set nocount off
output
1/1/2011 Monday 7 Week_day
1/2/2011 Tuesday 1 Week_day

Grouping date periods (by number of days) but exclude the weekends?

I have a table with start and end dates in. My goal is to have a table that has grouped these dates into how many days the period spans. I thought I had the solution with a simple SQL statement (MS SQL Server 2005) but I want to exclude weekends.
SELECT DATEDIFF(D, StartDate, EndDate)+1 AS Days,
COUNT(ID) as Count
FROM myDateTable
GROUP BY DATEDIFF(D, StartDate, EndDate)
This gives a record set of:
Days Count
1 4
2 2
4 1
7 2
Is this possible to exclude the weekends in the SQL statement and if not can it be done using ASP and a array perhaps?
Well then, using Sql Server 2005, you can try something like
DECLARE #Table TABLE(
ID INT,
StartDate DATETIME,
EndDate DATETIME
)
INSERT INTO #Table (ID,StartDate,EndDate) SELECT 1, '25 Jan 2009', '31 Jan 2009'
INSERT INTO #Table (ID,StartDate,EndDate) SELECT 2, '01 Jan 2009', '07 Jan 2009'
INSERT INTO #Table (ID,StartDate,EndDate) SELECT 3, '01 Jan 2009', '14 Jan 2009'
DECLARE #MinDate DATETIME,
#MaxDate DATETIME
SELECT #MinDate = MIN(StartDate) ,
#MaxDate = MAX(EndDate)
FROM #Table
--Create a temp result set between the Min and Max dates, with all dates, and their weekday names
;WITH DayValues AS(
SELECT #MinDate DateVal,
DATENAME(dw, #MinDate) DateValName
UNION ALL
SELECT DateVal + 1,
DATENAME(dw, DateVal + 1) DateValName
FROM DayValues
WHERE DateVal + 1 <= #MaxDate
),
--select the count of days for each StartDate and EndDate pair, excluding Saturdays and Sundays
DateCounts AS(
SELECT ID,
(
SELECT COUNT(1)
FROM DayValues
WHERE DateVal BETWEEN StartDate AND EndDate
AND DateValName NOT IN ('Saturday', 'Sunday')
) DateCount
FROM #Table
)
--Now group and count
SELECT DateCount,
COUNT(ID) TotalCount
FROM DateCounts
GROUP BY DateCount
OPTION (MAXRECURSION 0)
Output
DateCount TotalCount
----------- -----------
5 2
10 1
EDIT: Brief Explenation
You need to determine the number of days between (and including) 2 dates, that are not weekends.
So using a CTE, I create a temporary result set of dates ebwteen the Min and Max dates, and their Weekday Name (eg Monday, Tuesday... Sunday).
Then, for each of your date pairs, I count the number of entries that does not correspond to Saturday and Sunday.
Here's an ASP function that counts days weekdays between two dates.
<%
Dim StartDate, EndDate
StartDate = CDate("1/1/2010")
EndDate = CDate("2/1/2010")
Response.Write(WeekDayCount(StartDate, EndDate))
Function WeekDayCount(StartDate, EndDate)
dim tempDate, dayCount
tempDate = StartDate
dayCount = 0
'Step forward one day, counting non-week days
While tempDate <> EndDate
'The 1 and 7 might need to be tweaked depending on the locale of your
'server. 1 = Sunday, 7 = Saturday
If DatePart("w", tempDate) <> 1 And DatePart("w", tempDate) <> 7 Then
dayCount = dayCount + 1
End If
tempDate = DateAdd("d", 1, tempDate)
Wend
WeekDayCount = dayCount
End Function
%>
Have a look at ##DATEFIRST,
and look a this;
SELECT DATEPART(DW,GETDATE()).
You should be able to run a query WHERE the 'DW' is not equal to the weekend numbers.

Resources