Converting date - sql-server

I am looking to convert the following date
select (CONVERT(varchar(11), DATEADD(year, + 1,CONVERT(varchar(11), getdate(),111)))),datename(month,getdate()) for it returns 'jun 26 2015' ,'JUne'
I needed it to return as the following '2015/6' , June
This gives me what I want when using just get date
select CONVERT(varchar(7), getdate(), 111) = 2014/06
but I need it +1 year on my timewait field.
My actual code is as follows:
SELECT c.account_owner_by_SalesDiv, CONVERT(varchar(7), a.timeWait, 111)as rollingdate, DATENAME(MOnth, a.timeWait)
FROM SalesForce.dbo.SalesForceContact AS b INNER JOIN
Dossier_Replication.dbo.vw_SFAD_Contact_data AS c ON b.ContactID = c.CONTACTID__C RIGHT OUTER JOIN
satVRS.dbo.rptNECACallHistory AS a ON b.UserID = a.UserID_Caller
WHERE (b.Platform = 'HandsonVRS') AND (a.timeWait BETWEEN CONVERT(varchar(10), DATEADD(year, - 1, CONVERT(varchar, DATEADD(month, DATEDIFF(month, 0,
GETDATE()-1), 0), 120)), 120) AND CONVERT(varchar, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0), 101)) AND (a.isReport = '1') AND (a.NECA_isReport = '1') AND
(a.ConvoTime > '0') AND (c.account_owner_by_SalesDiv IN ('Enterprise Account', 'National Account', 'Major Account', 'House Account', 'Inside Sales Account'))
GROUP BY c.account_owner_by_SalesDiv, CONVERT(varchar(7), a.timeWait, 111), DATENAME(MOnth, a.timeWait)
order by rollingdate
Thanks in advance.

This gives 2015/06, June:
SELECT
CONVERT(VARCHAR(7), DATEADD(YEAR,1,GETDATE()), 111) + ', ' + DATENAME(MONTH,GETDATE())
If you don't want the leading zero you could use the SUBSTRING function to trim it.

Related

Return 6 previous Month select last month

SELECT DISTINCT itemcode,
itemdescription,
unitofmeasure,
Sum([current]) AS [Ending Balance]
WHERE transactiondate >= Dateadd(month, Datediff(month, 0,
Dateadd(m, -6, '2019-01-31'
)), 0)
Your code is missing FROM and GROUP BY clause and the Date Diff (Six Mont considering End Date = '2019-01-31') can be calculated as shown in the below script-
SELECT
itemcode,
itemdescription,
unitofmeasure,
Sum([current]) AS [Ending Balance]
FROM <your_table> -- FROM is missing. Please add appropriate table name
WHERE transactiondate Between
DATEADD(month, -6, '2019-01-31')
AND '2019-01-31'
GROUP BY itemcode,itemdescription,unitofmeasure
-- GROUP BY is required as you applied Aggregation on column [current]
If you wants only records from the 6th Previous month, WHERE condition will be as below-
WHERE YEAR(transactiondate) = YEAR(DATEADD(month, -6, '2019-01-31'))
AND MONTH(transactiondate) = MONTH(DATEADD(month, -6, '2019-01-31'))
For getting results from last 6 month, WHERE will be as below-
WHERE transactiondate BETWEEN
CAST(LEFT(CAST(DATEADD(MM,-6,CAST('2019-01-31' AS DATE)) AS VARCHAR),7) + '-01' AS DATE)
AND
DATEADD(DD,-1,CAST(CAST(LEFT(CAST(CAST('2019-01-31' AS DATE) AS VARCHAR),7) + '-01' AS DATE) AS DATE))
-- This is basically: transactiondate BETWEEN '2018-07-01' AND '2018-12-31'

How to get whole month or week or day level data in a single query

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

Get date with some criteria in SQL server

What I want is the EmployeeName from the emp_mst table with some condition which is given below:-
All EmployeeName for last 7 months from the current date and also less 15 days.
from the below query I am getting the result for the last month, but I want this for the last 6 months
select DATEADD(month, -1, GETDATE()- 15)
I am using sql server 2008
UPDATED PROCEDURE
SELECT * FROM (SELECT CASE
WHEN (SELECT Isnull(Sum(total_day), 0)
FROM xxacl_erp_ab_pl_count_view
WHERE emp_card_no = em.emp_card_no) > 7 THEN
'DOC Exteded By 1 month. Reason:- Taken leave='
+ CONVERT(VARCHAR, (SELECT Sum(total_day) FROM
xxacl_erp_ab_pl_count_view
WHERE emp_card_no = em.emp_card_no))
+
' which is > 7. Actual DOC='
+ CONVERT(VARCHAR, Dateadd(mm, em.probation_period, em.date_of_joining), 103)
+ ''
ELSE 'N/A'
END Remark,
em.*
FROM emp_mst em
LEFT JOIN company_mst comp
ON em.comp_mkey = comp.mkey
AND comp.fa_year = 2008
AND company_name NOT LIKE '%HELIK%'
WHERE em.status IN ( 'A' ) --and em.emp_type='E'
AND em.emp_card_no != 9999
AND em.resig_date IS NULL
AND CONVERT(DATETIME, em.date_of_joining, 103) >=
CONVERT(DATETIME,
Dateadd(m, -6, Getdate()), 103)
AND em.emp_card_no NOT IN (SELECT emp_card_no
FROM p_emp_confirmation_hdr
WHERE delete_flag = 'N'
AND hr_flag = 'Y')) pp
WHERE remark = 'N/A'
Casting to date to avoid calculating with timestamps
WHERE
yourdate >= dateadd(m, -6, datediff(d, 15, getdate())) and
yourdate < dateadd(d, -15, datediff(d, 0, getdate()))
Changed answer to adjust for you using sqlserver 2005 or older
Added 15 days extra to the interval
SELECT [emp_name]
FROM [TABLE]
WHERE [DateColumn] BETWEEN DATEADD(MONTH, -6, CAST(GETDATE() AS DATE))
AND DATEADD(DAY, -15, CAST(GETDATE() AS DATE))
This will show you employees who were added between six months ago and 15 days ago, for example, running that today would give you employees from the range 2014-12-24 and 2015-06-09.
EDIT: For SQL Server 2005 and earlier:
SELECT [emp_name]
FROM [TABLE]
WHERE [DateColumn] BETWEEN DATEADD(MONTH, -6, cast(convert(char(11), getdate(), 113) as datetime))
AND DATEADD(DAY, -15, cast(convert(char(11), getdate(), 113) as datetime))

how to get data of current week only in SQL server?

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

how to split the number of days in monthwise between two month

hi i want to find the total days of two month and split the days for month wise..for example...
26-02-2013 to 3-3-2013
here for the february month it shows 2days leave but march month i wont display the total leave..this is my query..can anyone correct my query..it shows only febraury days only,,march days is not shown here..
SELECT month(fdate) as Month_Number
, datename(month, fdate) as Month
, case when month(fdate) <> month(tdate) then
datediff(day, fdate, DATEADD(month, ((YEAR(fdate) - 1900) * 12) + MONTH(fdate), -1))
else
datediff(day, fdate, tdate)
end as Leaves
from test
where empid like '112'
Try this, it will display the number of days for each month:
SELECT *
, DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, t.fdate) + 1, 0)) Last_In_Month_Of_Beginning
, DATEADD(MONTH, DATEDIFF(MONTH, 0, t.tdate), 0) First_In_Month_Of_End
INTO #temp1
FROM test t
WHERE empid LIKE '112'
SELECT number Month_Number
, CASE
WHEN MONTH(fdate) = MONTH(tdate) THEN DATEDIFF(DAY, fdate, tdate) - 1
WHEN MONTH(Last_In_Month_Of_Beginning) = number THEN DATEDIFF(DAY, fdate, Last_In_Month_Of_Beginning)
WHEN MONTH(First_In_Month_Of_End) = number THEN DATEDIFF(DAY, First_In_Month_Of_End, tdate)
END Leave
INTO #temp2
FROM #temp1 a
JOIN master..spt_values v ON
v.type = 'P'
AND v.number BETWEEN MONTH(a.Last_In_Month_Of_Beginning) AND MONTH(a.First_In_Month_Of_End)
SELECT Month_Number
, SUM(Leave) Leaves
FROM #temp2
GROUP BY Month_Number
Here is an SQL Fiddle

Resources