January 19, 2018 at 9:16 am
Hi,
I need to get Year, monthname in three letters, startdate and enddate of the month for last two years based on today’s date. Any sample query please
January 19, 2018 at 9:39 am
KGJ-Dev - Friday, January 19, 2018 9:16 AMHi,I need to get Year, monthname in three letters, startdate and enddate of the month for last two years based on today’s date. Any sample query please
You should find it easy enough from here:
SELECT bom = DATEADD(day,1,DATEADD(month,-1,x.eom)), x.eom
FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),
(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24)) d (n)
CROSS APPLY (SELECT eom = DATEADD(MONTH,-n,EOMONTH(GETDATE()))) x
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 19, 2018 at 10:30 am
KGJ-Dev - Friday, January 19, 2018 9:16 AMHi,I need to get Year, monthname in three letters, startdate and enddate of the month for last two years based on today’s date. Any sample query please
You need to tell us how you intend to use the result. It WILL make a difference in what the EndDate should actually be (End of Month may NOT be the right value for this). I know you may not understand that right now so tell us how you're going to use the result so we can address that for you.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 19, 2018 at 11:23 am
Hi Chris and Jeff thank you for the replies. i will hold these values in temp table and will be using it for further use in joining with other tables. those first and last date of month will be required for other operations.
Hi Chris, i got the below error. I am usingg sqlserver 2008R2
Msg 195, Level 15, State 10, Line 7
'EOMONTH' is not a recognized built-in function name
January 19, 2018 at 11:57 am
KGJ-Dev - Friday, January 19, 2018 11:23 AMHi Chris and Jeff thank you for the replies. i will hold these values in temp table and will be using it for further use in joining with other tables. those first and last date of month will be required for other operations.Hi Chris, i got the below error. I am usingg sqlserver 2008R2
Msg 195, Level 15, State 10, Line 7
'EOMONTH' is not a recognized built-in function name
EOMONTH was added in SQL 2012. Chris' formula will also produce incorrect results whenever the current month does NOT have 31 days. Try making the following modification to his code.
SELECT bom = DATEADD(day,1,DATEADD(month,-1,x.eom)), x.eom
FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),
(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24)) d (n)
CROSS APPLY (SELECT eom = DATEADD(MONTH,DATEDIFF(MONTH, -1, GETDATE()) - n, -1)) x
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
January 19, 2018 at 12:18 pm
Hi Drew,
Thank you and it's working. is it possible to pass the 24 as value? this 24 may change and it might be 18 or some other number. i will get this number as parameter to my proc. is there any way to make this 24 as variable number.
January 19, 2018 at 12:35 pm
KGJ-Dev - Friday, January 19, 2018 12:18 PMHi Drew,
Thank you and it's working. is it possible to pass the 24 as value? this 24 may change and it might be 18 or some other number. i will get this number as parameter to my proc. is there any way to make this 24 as variable number.
What have you tried?
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
January 19, 2018 at 12:35 pm
drew.allen - Friday, January 19, 2018 11:57 AMKGJ-Dev - Friday, January 19, 2018 11:23 AMHi Chris and Jeff thank you for the replies. i will hold these values in temp table and will be using it for further use in joining with other tables. those first and last date of month will be required for other operations.Hi Chris, i got the below error. I am usingg sqlserver 2008R2
Msg 195, Level 15, State 10, Line 7
'EOMONTH' is not a recognized built-in function nameEOMONTH was added in SQL 2012. Chris' formula will also produce incorrect results whenever the current month does NOT have 31 days. Try making the following modification to his code.
SELECT bom = DATEADD(day,1,DATEADD(month,-1,x.eom)), x.eom
FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),
(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24)) d (n)
CROSS APPLY (SELECT eom = DATEADD(MONTH,DATEDIFF(MONTH, -1, GETDATE()) - n, -1)) x
Drew
When I run this I get 10 beginning of month dates that look wrong to me.
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
January 19, 2018 at 12:57 pm
Hi Drew,
below is my try DECLARE @EndDate DATETIME
,@StartDate DATETIME
,@Duration TINYINT = 15;
SET @EndDate = getdate();
SET @StartDate = DATEADD(MM, - @Duration, @EndDate)
SELECT UPPER(convert(VARCHAR(3), datename(month, DATEADD(MM, DATEDIFF(MM, 0, @EndDate) - N, 0)))) Month
,DATEADD(MM, DATEDIFF(MM, 0, @EndDate) - N, 0) startdate
,DATEADD(MM, DATEDIFF(MM, 0, @EndDate) - N + 1, 0) - 1 enddate
FROM (
VALUES (0)
,(1)
,(2)
,(3)
,(4)
,(5)
,(6)
,(7)
,(8)
,(9)
,(10)
,(11)
,(12)
,(13)
,(14)
,(15)
,(16)
,(17)
,(18)
,(19)
,(20)
,(21)
,(22)
,(23)
,(24)
) x(N)
WHERE N <= DATEDIFF(MONTH, @StartDate, @EndDate);
I am not sure how do i apply this on your query. because you have used cross apply to calculate EOM. Curious to know about how can i achieve the same in your query
January 19, 2018 at 1:12 pm
All you have to set / pass in is the "@number_of_months", although you could pass in a different ending month if you wanted to.
DECLARE @number_of_months int
SET @number_of_months = 24
--------------------------------------------------------------------------------
DECLARE @start_month date
DECLARE @end_month date
--SET @end_month = '20171113'
SET @end_month = ISNULL(@end_month, GETDATE())
SET @start_month = DATEADD(MONTH, DATEDIFF(MONTH, 0, @end_month) - @number_of_months + 1, 0)
;WITH
cteTally10 AS (
SELECT * FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS numbers(number)
),
cteTally100 AS (
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS number
FROM cteTally10 c1
CROSS JOIN cteTally10 c2
)
SELECT YEAR(first_day_of_month) AS year, CAST(DATENAME(MONTH, first_day_of_month) AS char(3)) AS mon,
first_day_of_month, DATEADD(DAY, -1, DATEADD(MONTH, 1, first_day_of_month)) AS last_day_of_month
FROM cteTally100 months
CROSS APPLY (
SELECT DATEADD(MONTH, months.number - 1, @start_month) AS first_day_of_month
) AS ca1
WHERE months.number <= @number_of_months
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
January 19, 2018 at 2:17 pm
Thank you scott for sharing another way of doing this.
January 19, 2018 at 4:49 pm
KGJ-Dev - Friday, January 19, 2018 11:23 AMHi Chris and Jeff thank you for the replies. i will hold these values in temp table and will be using it for further use in joining with other tables. those first and last date of month will be required for other operations.Hi Chris, i got the below error. I am usingg sqlserver 2008R2
Msg 195, Level 15, State 10, Line 7
'EOMONTH' is not a recognized built-in function name
"Required for other operations" doesn't tell me much. What kind of operations? Are you talking about using the start and end dates as join criteria to group dated rows? If so, EOMONTH is the wrong way to go. You should also bullet proof your code by anticipating someone using using times in the date columns of those "other tables in those other operations" as well. So, what are the "other operations" that you're talking about. Be specific about the method(s) that will be using the start and end dates.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 22, 2018 at 8:47 am
drew.allen - Friday, January 19, 2018 11:57 AMKGJ-Dev - Friday, January 19, 2018 11:23 AMHi Chris and Jeff thank you for the replies. i will hold these values in temp table and will be using it for further use in joining with other tables. those first and last date of month will be required for other operations.Hi Chris, i got the below error. I am usingg sqlserver 2008R2
Msg 195, Level 15, State 10, Line 7
'EOMONTH' is not a recognized built-in function nameEOMONTH was added in SQL 2012. Chris' formula will also produce incorrect results whenever the current month does NOT have 31 days. Try making the following modification to his code.
SELECT bom = DATEADD(day,1,DATEADD(month,-1,x.eom)), x.eom
FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),
(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24)) d (n)
CROSS APPLY (SELECT eom = DATEADD(MONTH,DATEDIFF(MONTH, -1, GETDATE()) - n, -1)) x
Drew
DECLARE @getdate-2 DATETIME = '20170228'
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 22, 2018 at 9:48 am
This may be 'the long way around the barn', but it works. You could set this up a a SP and pass it the number of months you want and a start date(current date).
DECLARE @months AS INT;
DECLARE @startdate AS DATE;
DECLARE @Begindate AS DATE;
DECLARE @datetable TABLE
(
Year_int INT,
Month_Int INT,
Month_Abv CHAR(3),
BOM_Date DATE,
EOM_Date DATE
)
;
SET @months = 30;
SET @startdate = GETDATE();
SET @Begindate = (SELECT DATEADD(dd, -(DATEPART(dd, @startdate) - 1), @startdate));
LOOPHERE:
INSERT INTO @datetable
SELECT YEAR(@Begindate) AS Year_int,
MONTH(@Begindate) AS Month_Int,
LEFT(CONVERT(CHAR(11), @Begindate, 107), 3) AS Month_Abv,
@Begindate AS BOM_Date,
DATEADD(dd, -1, DATEADD(MM, 1, @Begindate)) AS EOM_Date
;
SET @months = @months -1;
SET @Begindate = (SELECT DATEADD(mm, -1, @Begindate));
IF @months > 0
GOTO LOOPHERE;
SELECT *
FROM @datetable
;
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
January 22, 2018 at 9:54 am
below86 - Monday, January 22, 2018 9:48 AMThis may be 'the long way around the barn', but it works. You could set this up a a SP and pass it the number of months you want and a start date(current date).
DECLARE @months AS INT;
DECLARE @startdate AS DATE;
DECLARE @Begindate AS DATE;DECLARE @datetable TABLE
(
Year_int INT,
Month_Int INT,
Month_Abv CHAR(3),
BOM_Date DATE,
EOM_Date DATE
)
;SET @months = 30;
SET @startdate = GETDATE();SET @Begindate = (SELECT DATEADD(dd, -(DATEPART(dd, @startdate) - 1), @startdate));
LOOPHERE:
INSERT INTO @datetable
SELECT YEAR(@Begindate) AS Year_int,
MONTH(@Begindate) AS Month_Int,
LEFT(CONVERT(CHAR(11), @Begindate, 107), 3) AS Month_Abv,
@Begindate AS BOM_Date,
DATEADD(dd, -1, DATEADD(MM, 1, @Begindate)) AS EOM_Date
;SET @months = @months -1;
SET @Begindate = (SELECT DATEADD(mm, -1, @Begindate));
IF @months > 0
GOTO LOOPHERE;
SELECT *
FROM @datetable
;
A loop is horribly inefficient compared to the tally table method outlined earlier in this thread.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 15 posts - 1 through 15 (of 104 total)
You must be logged in to reply to this topic. Login to reply