August 31, 2011 at 9:25 am
Hi,
I want to create a temp table and insert all the list of
monthorder , Monthnames , year
1 january 2011
2 february 2011
.
.
.
12 December 2011
Thanks,
Komal
August 31, 2011 at 9:40 am
I got query to get month order and year but not getting month names.
SELECT [year], [month]
FROM (
SELECT 2005 AS [year] UNION ALL
SELECT 2006 UNION ALL
SELECT 2007 UNION ALL
SELECT 2008 UNION ALL
SELECT 2009 UNION ALL
SELECT 2010 UNION All
SELECT 2011 UNION All
SELECT 2012 UNION ALL
SELECT 2013
) years
CROSS JOIN (
SELECT 1 AS [month] UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12
) monthsorder
ORDER BY years.[year], monthsorder.[month]
can anybody help getting monthnames in same query??? so that i can create table
August 31, 2011 at 9:54 am
SELECT [year], [month], datename(month,dateadd(mm,[month] -1,DATEADD(yy, DATEDIFF(yy,0,[year] - 1900), 0)))
FROM (
SELECT 2005 AS [year] UNION ALL
SELECT 2006 UNION ALL
SELECT 2007 UNION ALL
SELECT 2008 UNION ALL
SELECT 2009 UNION ALL
SELECT 2010 UNION All
SELECT 2011 UNION All
SELECT 2012 UNION ALL
SELECT 2013
) years
CROSS JOIN (
SELECT 1 AS [month] UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12
) monthsorder
ORDER BY years.[year], monthsorder.[month]
Lowell
August 31, 2011 at 11:29 am
In just a couple of years, this table is only going to contain past dates.
If you're going to create a calendar table, it should contain more years than this, I would have thought - or be dynamically created as and when required spanning enough years before and after the current date.
Also, Lowell's suggestion is needlessly complicated - the months have the same name every year, so there is no need to provide a variable year in the MonthName calculation:
selectt.Number as [year]
,m.[month]
,DateName(month,DateAdd(month,(m.[month]-1),0)) as MonthName
fromtally t
cross join(values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) m([month])
wheret.Number between DatePart(year,GetDate()) - 100 and DatePart(year,GetDate()) + 200
Of course, this assumes that you have a Tally table handy, but if you haven't, then you should! 😉
August 31, 2011 at 12:27 pm
If you dont have a tally table, you can utilize this code :
DECLARE @StartDATE DATETIME
SET @StartDATE = '01-01-1985'
;WITH Tens (N) AS
(
SELECT 0 N UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0
),
Thousands (N) AS
(
SELECT t1.N FROM Tens t1 CROSS JOIN Tens t2 CROSS JOIN Tens t3
),
Tally AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) N FROM Thousands
),
Month_Numbers ( MonthNum , Month_Name) AS
(
SELECT 1 , 'January'
UNION ALL SELECT 2 , 'February'
UNION ALL SELECT 3 , 'March'
UNION ALL SELECT 4 , 'April'
UNION ALL SELECT 5 , 'May'
UNION ALL SELECT 6 , 'June'
UNION ALL SELECT 7 , 'July'
UNION ALL SELECT 8 , 'August'
UNION ALL SELECT 9 , 'September'
UNION ALL SELECT 10 , 'October'
UNION ALL SELECT 11 , 'November'
UNION ALL SELECT 12 , 'December'
)
SELECT Nums.MonthNum , DATEPART(YY,DATEADD(M,N-1,@StartDATE)) [YEAR], DATENAME(MM,DATEADD(M,N-1,@StartDATE)) [MONTH]
FROM Tally T
JOIN Month_Numbers Nums
ON DATENAME(MM,DATEADD(M,N-1,@StartDATE)) = Nums.Month_Name
WHERE DATEPART(YY,DATEADD(M,N-1,@StartDATE)) <= YEAR(GETDATE())
The @StartDate will specify from what year your output should start.
August 31, 2011 at 12:33 pm
A more cleaner version:
DECLARE @StartDATE DATETIME
SET @StartDATE = '01-01-1985'
;WITH Tens (N) AS
(
SELECT 0 N UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0
),
Thousands (N) AS
(
SELECT t1.N FROM Tens t1 CROSS JOIN Tens t2 CROSS JOIN Tens t3
),
Tally AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) N FROM Thousands
)
SELECT MonthNum =
CASE T.N % 12
WHEN 0 THEN 12
ELSE T.N % 12
END
, DATEPART(YY,DATEADD(M,N-1,@StartDATE)) [YEAR]
, DATENAME(MM,DATEADD(M,N-1,@StartDATE)) [MONTH]
FROM Tally T
WHERE DATEPART(YY,DATEADD(M,N-1,@StartDATE)) <= YEAR(GETDATE())
August 31, 2011 at 12:49 pm
Thank you for all responses...I got it!!!
September 2, 2011 at 8:19 pm
komal145 (8/31/2011)
Thank you for all responses...I got it!!!
Cool! Please post the code you ended up using so that we all may learn. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
September 2, 2011 at 11:18 pm
WITH DateYear AS
(
SELECT 0 AS num
UNION ALL
SELECT num + 1 FROM DateYear
WHERE num < 11
)
SELECT CONVERT(DATE,DATEADD(MONTH,num,'2011')) AS Date from DateYear
September 3, 2011 at 4:52 am
Very cool code @ColdCoffee.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
September 4, 2011 at 1:16 am
Welsh Corgi (9/3/2011)
Very cool code @ColdCoffee.
Thank you Welsh!
September 4, 2011 at 7:50 am
For even cleaner code, your method needs a 0-based tally table. This is easily accomplished by adding -1 to row_number(). Your code then looks like this:
DECLARE @StartDATE DATETIME;
SET @StartDATE = {d '1985-01-01'};
;WITH Tens (N) AS
(
SELECT 0 N UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0
),
Thousands (N) AS
(
SELECT t1.N FROM Tens t1 CROSS JOIN Tens t2 CROSS JOIN Tens t3
),
Tally AS
(
SELECT -1 + ROW_NUMBER() OVER (ORDER BY (SELECT 0)) N FROM Thousands
)
SELECT MonthNum = T.N
, DATEPART(YY,DATEADD(M,T.N,@StartDATE)) [YEAR]
, DATENAME(MM,DATEADD(M,T.N,@StartDATE)) [MONTH]
FROM Tally T
WHERE DATEPART(YY,DATEADD(M,T.N,@StartDATE)) <= YEAR(GETDATE());
September 4, 2011 at 9:32 am
R.P.Rozema (9/4/2011)
This is easily accomplished by adding -1 to row_number().
Thanks Rozema for "bettering" it out. If zero based tally table will make it cleaner, we can do something like
Tally AS
(
SELECT 0
UNION ALL
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) N FROM Thousands
)
This way, we can stop the Compute Scalar operator and could the engine from calculating for all the rows. Right, R.P.R ?
September 4, 2011 at 9:35 am
And coming to my code, a 0-based will throw the CASE statement out of sync. That's why i din't use it 🙂
September 4, 2011 at 9:48 am
Added more details in code and optimized it.
DECLARE @StartDATE DATETIME
,@EndDate DATETIME
,@YearofEndDate INT
SELECT @StartDATE = '01-01-1985' , @EndDate = GETDATE()
SET @YearofEndDate = YEAR(@EndDate)
;WITH Tens (N) AS
(
SELECT 0 N UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0
),
Thousands (N) AS
(
SELECT t1.N FROM Tens t1 CROSS JOIN Tens t2 CROSS JOIN Tens t3
),
Numbers AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) N FROM Thousands
)
SELECT MonthNum =
CASE T.N % 12
WHEN 0 THEN 12
ELSE T.N % 12
END
, DATEPART(YY,DATEADD(M,N-1,@StartDATE)) [Year]
, DATENAME(MM,DATEADD(M,N-1,@StartDATE)) [Month]
, DATEADD(M,N-1,@StartDATE) [StartDateOfMonth]
, DATEADD (DAY , -1, DATEADD(MONTH , 1 , DATEADD(M,N-1,@StartDATE)) ) EndDateOfMonth
FROM Numbers T
WHERE DATEPART(YY,DATEADD(M,N-1,@StartDATE)) <= @YearofEndDate
ORDER BY [Year] , MonthNum
Viewing 15 posts - 1 through 15 (of 29 total)
You must be logged in to reply to this topic. Login to reply