Get Year of parameter dates - sql

Is there a way for me to get the year of the months based on the parameter given?
DECLARE #StartDate DATETIME,
#EndDate DATETIME;
SELECT
#StartDate = '2016-02-01',
#EndDate = '2017-03-01';
SELECT
/*is there a way for me to get the year of months based on the parameter dates*/
/*DATEPART(YEAR, DATEADD(YEAR, x.number, #StartDate)) AS Year,*/
DATEPART(MONTH, DATEADD(MONTH, x.number, #StartDate)) AS Month,
DATENAME(MONTH, DATEADD(MONTH, x.number, #StartDate)) AS MonthName
FROM
master.dbo.spt_values x
WHERE
x.type = 'P'
AND x.number <= DATEDIFF(MONTH, #StartDate, #EndDate)
The result of my query....
What I want is that from february to december it'll have a year of 2016 and from january to march it'll have a 2017 year..... based on the parameter that I've given..
Thanks in advance.

Try this:
SELECT YEAR(y.dt) AS Year,
MONTH(y.dt) AS Month,
DATENAME(MONTH, y.dt) AS MonthName
FROM master.dbo.spt_values x
CROSS APPLY (SELECT DATEADD(MONTH, x.number, #StartDate)) AS y(dt)
WHERE x.type = 'P'
AND x.number <= DATEDIFF(MONTH, #StartDate, #EndDate)
Demo here

Related

How to extract data from 1st day of the month till today's date -1

I am trying to extract all the data that has the datetime from 1st day of the month till yesterday, for example:
01/06/2017 - 22/06/2017
I have used this code:
Select *
from testDb.dbo.Company1
WHERE MONTH(CreatedDate) = MONTH(dateadd(dd, -1, GetDate())) AND
YEAR(CreatedDate) = YEAR(dateadd(dd, -1, GetDate()))
EDIT
My column for CreatedDate, its data type is DateTime. Not sure if there is any difference tho.
But this prints out all the data from 01/06/2017 - 23/06/2017. What code should I write such that it will print all data till (today's date-1)? Thanks for the help and have a great day!
Instead of comparing the month and year components separately, try bounding your result set using a range of two complete data points, one being the start of the month, and the other being yesterday.
SELECT *
FROM testDb.dbo.Company1
WHERE
CreatedDate BETWEEN DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) AND
DATEADD(day, -1, CAST(GETDATE() AS date))
Demo
Try this query --
SELECT *
FROM testDb.dbo.Company1
WHERE DATEPART(Month, CreatedDate) = Datepart(Month, GETDATE())
AND DATEPART(Day, CreatedDate) <= DATEPART(Day, Getdate())
Edit:
SELECT *
FROM testDb.dbo.Company1
WHERE DATEPART(Month, CreatedDate) = Datepart(Month, GETDATE())
AND DATEPART(Day, CreatedDate) < DATEPART(Day, Getdate())
Edit 2:
SELECT CONVERT(VARCHAR(30),CreatedDate,120) As [CreatedDate]
FROM testDb.dbo.Company1
WHERE DATEPART(Month, CreatedDate) = Datepart(Month, GETDATE())
AND DATEPART(Day, CreatedDate) < DATEPART(Day, Getdate())
This though wont work when today is the first day of the month
SELECT *
FROM your_table
WHERE createdDate BETWEEN
CAST('1 ' + DateName(month, GetDate()) + ' ' +Year(GetDate()) as datetime)
AND DATEADD(day, -1, GETDATE() )
I think this where clause does what you want:
where createdDate >= dateadd(month, 0, datediff(month, 0, getdate())) and
createdDate < cast(getdate() as date)

Accumulating data from previous year

I'm having trouble with finding a solution for below SQL-query. My first query should and does the trick of accumulating amount of last year's data.
CASE
WHEN rehuv.VER_DATUM >= dateadd(year, -1, dateadd(month, -datepart(month, #STARTDATE) + 1, #STARTDATE))
AND rehuv.VER_DATUM <= DATEADD(year, -1, #ENDDATE) THEN rehuv.BELOPP ELSE 0
END AS PREVIOUS_YEAR_ACC
But when I try to accumulate data within a specific time interval (I want the same time interval as #startdate -> #enddate but one year backwards). Sql-query:
CASE
WHEN rehuv.VER_DATUM >= DATEADD(year, -1, #STARTDATE)
AND rehuv.VER_DATUM <= DATEADD(year, -1, #ENDDATE) THEN rehuv.BELOPP ELSE 0
END AS PREVIOUS_YEAR_MONTH
Thank you in advance!
Best regards,
Simon
If you want the monthly sum of last year, you can try this:
DECLARE #STARTDATE DATETIME;
DECLARE #ENDDATE DATETIME;
SET #STARTDATE='2015-1-1';
SET #ENDDATE='2015-5-1';
DECLARE #LAST_YEAR DATE;
SET #LAST_YEAR = DATEADD(YEAR, -1, DATEADD(DAY,
DATEPART(DAYOFYEAR, GETDATE())*-1+1, CONVERT(DATE, GETDATE())));
SELECT DATEADD(MONTH, DATEPART(MONTH, VER_DATUM), #LAST_YEAR) MONTH, SUM(CASE
WHEN REHUV.VER_DATUM >= DATEADD(YEAR, -1, #STARTDATE)
AND REHUV.VER_DATUM <= DATEADD(YEAR, -1, #ENDDATE)
THEN REHUV.BELOPP ELSE 0
END ) AS PREVIOUS_YEAR_MONTH
FROM REHUV
GROUP BY DATEPART(MONTH, VER_DATUM)

query for first and last day of month

If I have the name of the month, how can I have the first and last day of that month in SQL?
I have this query to returns the month names:
DECLARE #StartDate DATETIME,
#EndDate DATETIME;
SELECT #StartDate = '20110501'
,#EndDate = '20110801';
SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, #StartDate)) AS MonthName
FROM master.dbo.spt_values x
WHERE x.type = 'P'
AND x.number <= DATEDIFF(MONTH, #StartDate, #EndDate)
Result:
Now, how can i get the first and last day of that months? changing the query.
If you want a more general and simple solution:
DECLARE #startdate AS DATETIME = GETDATE()
SELECT
DATEADD(MONTH, DATEDIFF(MONTH, 0, #startdate) , 0) as startMonth,
DATEADD(SECOND, -1, DATEADD(MONTH, 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, #startdate) , 0) ) ) as endMonth
Gives the result:
startMonth endMonth
----------------------- -----------------------
2013-06-01 00:00:00.000 2013-06-30 23:59:59.000
Try this :-
DECLARE #StartDate DATETIME,
#EndDate DATETIME;
SELECT #StartDate = '20110501'
,#EndDate = '20110801';
SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, #StartDate)) AS MonthName,
CONVERT(VARCHAR(25),
DATEADD(dd,-(DAY(DATEADD(MONTH, x.number, #StartDate))-1),DATEADD(MONTH, x.number, #StartDate)),101) as FirstDay,
CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,DATEADD(MONTH, x.number, #StartDate)))),DATEADD(mm,1,DATEADD(MONTH, x.number, #StartDate))),101) as LastDay
FROM master..spt_values x
WHERE x.type = 'P'
AND x.number <= DATEDIFF(MONTH, #StartDate, #EndDate)
Result :-
MonthName FirstDay LastDay
May 05/01/2011 05/31/2011
June 06/01/2011 06/30/2011
July 07/01/2011 07/31/2011
August 08/01/2011 08/31/2011
Result obtained taking the help from this query
This code gives you the first date of Current Month:
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
If you know the Month's number then:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + #MonthNumber, 0)
Same goes for Month's end date :
SELECT DATEADD(MILLISECOND, -1,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - ##MonthNumber+ 1, 0))

SQL - Get Numerical Day of Month/Quarter

Using SQL Server 2005:
How can I get the numerical day of the month and day of the quarter in a query?
DECLARE #DATE DATETIME
SET #DATE = GETDATE()
SELECT DATEPART(dy, #DATE) AS DayOfYear
--, <something> AS DayOfQuarter
--, <something> AS DayOfMonth
, DATEPART(dw, #DATE) AS DayOfWeek
Thanks in advance!
DECLARE #DATE DATETIME
SET #DATE = GETDATE()
SELECT DATEPART(dy, #DATE) AS DayOfYear
, DATEDIFF(d, DATEADD(qq, DATEDIFF(qq, 0, #DATE), 0), #DATE) + 1 AS DayOfQuarter
, DAY(#Date) AS DayOfMonth
, DATEPART(dw, #DATE) AS DayOfWeek
As for the day of quarter, this will demande a bit of further investigate on my side. Despite, I guess that for the day of month, this would simply be the date itself:
select DATEPART(D, #DATE)
SELECT
DATEPART(year, '12:10:30.123')
,DATEPART(month, '12:10:30.123')
,DATEPART(day, '12:10:30.123')
,DATEPART(dayofyear, '12:10:30.123')
,DATEPART(weekday, '12:10:30.123');

How do I compare against the current week using SQL Server?

How do I compare an SQL Server date column against the current week?
For instance:
WHERE [Order].SubmittedDate = *THIS WEEK*
You could convert your date to a week number and compare this to the week number from the current date. Likewise, you'll need to compare the year as well, so that you don't get last year's weeks.
WHERE DATEPART(wk, [Order].SubmittedDate) = DATEPART(wk, GETDATE())
AND DATEPART(yy, [Order].SubmittedDate) = DATEPART(yy, GETDATE())
Assuming you are meaning always "this week" and there are no records with Submitted dates in the future, which I imagine could be the case you can do:
WHERE [Order].SubmittedDate >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
If dates do go into the future, the full restriction to this week is:
WHERE [Order].SubmittedDate >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND [Order].SubmittedDate < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
I'd strongly recommend using a clause based on a start and end date like this, as it will allow efficient index use so should perform better.
Try this:
WHERE [Order].SubmittedDate BETWEEN
DATEADD(d, - DATEPART(dw, GETDATE()) + 1, GETDATE()) AND
DATEADD(d, 7 - DATEPART(dw, GETDATE()) , GETDATE())
Maybe this can run faster, as doesn't needs to be evaluated everytime:
DECLARE #StartDate DATETIME,
#EndDate DATETIME
SELECT #StartDate = DATEADD(d, - DATEPART(dw, GETDATE()) + 1, GETDATE()),
#EndDate = DATEADD(d, 8 - DATEPART(dw, GETDATE()) , GETDATE())
-- // Strip time part, so week starts on Sunday 00:00
SELECT #StartDate = CAST(FLOOR(CAST(#StartDate AS FLOAT)) AS DATETIME),
#EndDate = CAST(FLOOR(CAST(#EndDate AS FLOAT)) AS DATETIME)
...
WHERE [Order].SubmittedDate >= #StartDate AND [Order].SubmittedDate < #EndDate