I have a data in which I have to display a record by month or week or day according to the date selected.
If the date selected is for a whole month for like january its 01-01-2015 to 31-01-2015
then it should fetch for the record for the whole month.
If the date selected is such that for example like 01-01-2015 to 15-02-2015. Now here there are more than a month but Feburary is not in whole month so the january will record will come in one and next first week record for Feburary will be fetched in one and next week record will be fetched in another`.
Also he day left (as there will be 14 days in week two) will have a another record.`
So, in all 4 record will be fetched. Here instead of fecthing whole 46 days record I am just fecthing 4 records. Hope I gave a proper example if you can't just comment I will explain with another exapmle. I am now just worrying about how to split month, week and day. Can anyone help me with that. Thanks in advance
I am only worried about how to split the dates in month week or a day. The formula is simple if e have whole month the entry should be shown in a single record. If we date between of more than week it should shown week wise and less than week it should be on day level
Input
Here Time_type_id is a foreign key from other 3 tables. This time_type_id will help to get data day, week or month wise. The data is shown according to date passed by the user. Time_id is the primary key of the table and it points to the id of other 3 fact table.
Dimesion Table
Time_type_id Time_id metric_id
1 1 100
1 2 400
1 3 300
1 4 100
1 5 250
2 6 1000
2 7 2000
2 8 3000
2 9 2000
2 10 5000
3 11 11000
3 12 20000
3 13 40000
3 14 50000
3 15 60000
Fact table
Fact1- This shows the day basis data.
Dim_type Id day_date month year
1 1 01-09-2015 September 2015
1 2 02-09-2015 September 2015
1 3 03-09-2015 September 2015
1 4 04-09-2015 September 2015
1 5 05-09-2015 September 2015
Fact2- This shows the weekly basis data.
Dim_type Id week_number month year
2 6 week1 September 2015
2 7 week2 September 2015
2 8 week3 September 2015
2 9 week4 September 2015
2 10 week1 October 2015
Fact3- This shows the monthly basis data.
Dim_type Id month year
3 11 September 2015
3 12 September 2015
3 13 September 2015
3 14 September 2015
3 15 October 2015
Now the user just passed the dates between which he wants to see the data and according to dates passed we have to sho the ouput.
If it is month level we have to show Fact3 table where Dim_type is 3 same for other two table. I just want how to split the the date
Output-
For second example-
Sr no id data (from the date range)
1 1 data from whole January
2 2 data from first week of Feburary
3 3 data from second week of Feburary
4 4 data from 15 Febuaray
Note: Don't make that a week will always have 7 days maybe if week start from Thrusday than it would not have 7 days
you can use two parameter, so it's dynamic to choose dates like:
Declare #FromDate DATETIME,
#ToDate DATETIME
SELECT ...
FROM ...
WHERE DATE BETWEEN #FromDate AND #ToDate
There can be two solution available in your case:
Solution # 1
You need a table/temptable which contains data for all dates with columns like Date, DaySrNo, WeekSrNo, MonthSrNo and Moreover YearSrNo. Use this table in Join and get your data among three tables (which are also in JOIN)
Solution # 2
You need extra column for date range in your existing table structure:
like In Fact1 table contains day wise data, So you need Date Range Columns Like StartDate and EndDate
Same way Same Two column in other tables Fact2 and Fact3
So using this column in Join and where filter you can get your desired output.
IF OBJECT_ID('IAS_TEST_SCHEMA.DATESPLIT', 'P') IS NOT NULL
DROP PROCEDURE IAS_TEST_SCHEMA.DATESPLIT;
GO
CREATE PROCEDURE IAS_TEST_SCHEMA.DATESPLIT (#STARTDATE date, #ENDDATE date)
AS
BEGIN
DECLARE #DATEDIFF integer,
#MONTHEND integer = 0,
#MONTHSTART integer = 0,
#STARTWEEK integer = 0,
#ENDWEEK integer = 0;
IF #STARTDATE = CONVERT(date, DATEADD(M, DATEDIFF(M, 0, #STARTDATE), 0))
BEGIN
SET #MONTHSTART = 1;
END
IF MONTH(#ENDDATE) != MONTH(DATEADD(DAY, 1, #ENDDATE))
BEGIN
SET #MONTHEND = 1;
END
IF DATEPART(DW, #STARTDATE) = 2
BEGIN
SET #STARTWEEK = 1;
END
IF DATEPART(DW, #ENDDATE) = 1
BEGIN
SET #ENDWEEK = 1;
END
SET #DATEDIFF = DATEDIFF(DAY, #STARTDATE, #ENDDATE);
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
END
END AS START_DATE,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/'
+ DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #STARTDATE), 6)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN CASE
WHEN #MONTHEND = 0 THEN DATENAME(MONTH, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1)) + ' ' + DATENAME(YEAR, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1))
ELSE DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(S, -1, DATEADD(MM, DATEDIFF(M, 0, #STARTDATE) + 1, 0))))
END
END AS END_DATE
UNION
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE DATENAME(MONTH, CONVERT(date, DATEADD(M, DATEDIFF(M, -1, #STARTDATE), 0))) + ' '
+ DATENAME(YEAR, CONVERT(date, DATEADD(M, DATEDIFF(M, -1, #STARTDATE), 0)))
END
END,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/'
+ DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
END
WHEN #DATEDIFF > 90 THEN CASE
WHEN #MONTHEND = 0 THEN DATENAME(MONTH, CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1))) + ' '
+ DATENAME(YEAR, CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1)))
ELSE DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
END
END
UNION
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHEND = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DD, -(DAY(#ENDDATE) - 1), #ENDDATE)))
END
END,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHEND = 1 THEN DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
END
END
END;
GO
I want records from table which stores the current date when a record is inserted with in current week only.
I have tried:
SELECT PId
,WorkDate
,Hours
,EmpId
FROM Acb
WHERE EmpId=#EmpId AND WorkDate BETWEEN DATEADD(DAY, -7, GETDATE()) AND GETDATE()
Do it like this:
SET DATEFIRST 1 -- Define beginning of week as Monday
SELECT [...]
AND WorkDate >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND WorkDate < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
Explanation:
datepart(dw, getdate()) will return the number of the day in the current week, from 1 to 7, starting with whatever you specified using SET DATEFIRST.
dateadd(day, 1-datepart(dw, getdate()), getdate()) subtracts the necessary number of days to reach the beginning of the current week
CONVERT(date,getdate()) is used to remove the time portion of GETDATE(), because you want data beginning at midnight.
A better way would be
select datepart(ww, getdate()) as CurrentWeek
You can also use wk instead of ww.
Datepart Documentation
Its Working For Me.
Select * From Acb Where WorkDate BETWEEN DATEADD(DAY, -7, GETDATE()) AND DATEADD(DAY, 1, GETDATE())
You have to put this line After the AND Clause AND DATEADD(DAY, 1, GETDATE())
datepart(dw, getdate()) is the current day of the week, dateadd(day, 1-datepart(dw, getdate()), getdate()) should be the first day of the week, add 7 to it to get the last day of the week
You can use following query to extract current week:
select datepart(dw, getdate()) as CurrentWeek
SET DATEFIRST 1;
;With CTE
AS
(
SELECT
FORMAT(CreatedDate, 'MMMM-yyyy') as Months,
CASE
WHEN YEAR(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min(CreatedDate)), Min(CreatedDate))) < YEAR(Min(CreatedDate))
THEN FORMAT(DATEADD(YEAR, DATEDIFF(YEAR, 0,DATEADD(YEAR, 0 ,GETDATE())), 0) ,'MMM dd') + ' - ' + FORMAT(DATEADD(dd, 7-(DATEPART(dw, Min(CreatedDate))), Min(CreatedDate)) ,'MMM dd')
ELSE
FORMAT(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min(CreatedDate)), Min(CreatedDate)) ,'MMM dd') + ' - ' + FORMAT(DATEADD(dd, 7-(DATEPART(dw, Min(CreatedDate))), Min(CreatedDate)) ,'MMM dd')
END DateRange,
Sum(ISNULL(Total,0)) AS Total,
sum(cast(Duration as int)) as Duration
FROM TL_VriandOPI_Vendorbilling where VendorId=#userID and CompanyId=#CompanyID
Group By DATEPART(wk, CreatedDate) ,FORMAT(CreatedDate, 'MMMM-yyyy')
)
SELECT Months,DateRange,Total,Duration,
case when DateRange=(select FORMAT(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min(getdate())), Min(getdate())) ,'MMM dd') + ' - ' +
FORMAT(DATEADD(dd, 7-(DATEPART(dw, Min(getdate()))), Min(getdate())) ,'MMM dd'))
then 1 else 0 end as Thisweek
FROM CTE order by Months desc
Using DATEDIFF works as well, however a bit hacky since it doesn't care about datefirst:
set datefirst 1; -- set monday as first day of week
declare #Now datetime = '2020-09-28 11:00';
select *
into #Temp
from
(select 1 as Nbr, '2020-09-22 10:00' as Created
union
select 2 as Nbr, '2020-09-25 10:00' as Created
union
select 2 as Nbr, '2020-09-28 10:00' as Created) t
select * from #Temp where DATEDIFF(ww, dateadd(dd, -##datefirst, Created), dateadd(dd, -##datefirst, #Now)) = 0 -- returns 1 result
select * from #Temp where DATEDIFF(ww, dateadd(dd, -##datefirst, Created), dateadd(dd, -##datefirst, #Now)) = 1 -- returns 2 results
drop table #Temp