December 27, 2010 at 3:32 am
Hi friends,
I want to fill a dropdownlist in asp.net 2.0 with all months from january to December.I want to get the months name in a stored procedure and do my later process.
Plz help me how to auto generate months
thanks in advance,
kalai
December 27, 2010 at 3:56 am
This should do the trick:
WITH MonthNumbers ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
AS (
SELECT 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
)
SELECT MonthNumber, MonthName = DATENAME(month, DATEADD(month, MonthNumber - 1,0))
FROM (
SELECT *
FROM MonthNumbers MN
) AS src
UNPIVOT (M FOR MonthNumber IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) AS upvt
-- Gianluca Sartori
December 27, 2010 at 8:52 am
Here's a method that utilizes a small, recursive CTE:
;WITH Months (N, MyDate) AS
(
-- get the first day of the current year
-- N is the MonthNumber
SELECT 1, DateAdd(year, DateDiff(year, 0, GetDate()), 0)
UNION ALL
-- add one month recursively until we have 12 months
SELECT N+1, DateAdd(month, 1, MyDate)
FROM Months
WHERE N < 12
)
-- get the names of the months, order by month number
SELECT MonthName = DateName(month, MyDate)
FROM Months
ORDER BY N;
Edit: However, in comparing this solution to Gianluca's, his produces a tighter execution plan that consistently runs slightly faster.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
December 28, 2010 at 7:20 am
Maybe it's just me, but isn't this a solution in search of a problem? How often do the names of the months change? Just code the names in... (or am I missing something)
---------------------------------------------------------
How best to post your question[/url]
How to post performance problems[/url]
Tally Table:What it is and how it replaces a loop[/url]
"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
December 28, 2010 at 7:25 am
jcrawf02 (12/28/2010)
Maybe it's just me, but isn't this a solution in search of a problem? How often do the names of the months change? Just code the names in... (or am I missing something)
True, but... do you speak Italian, Jon?
Do you want to know the month names in Italian?
Run the above code after issuing "SET Language Italian" and see. 🙂
-- Gianluca Sartori
December 28, 2010 at 7:28 am
Gianluca Sartori (12/28/2010)
jcrawf02 (12/28/2010)
Maybe it's just me, but isn't this a solution in search of a problem? How often do the names of the months change? Just code the names in... (or am I missing something)True, but... do you speak Italian, Jon?
Do you want to know the month names in Italian?
Run the above code after issuing "SET Language Italian" and see. 🙂
Ah, good point. Stupid Americans. 😀
---------------------------------------------------------
How best to post your question[/url]
How to post performance problems[/url]
Tally Table:What it is and how it replaces a loop[/url]
"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
January 16, 2011 at 7:58 pm
You could also use this:
with MonthNumbers as (
select 1 as MonthNum union all
select 2 as MonthNum union all
select 3 as MonthNum union all
select 4 as MonthNum union all
select 5 as MonthNum union all
select 6 as MonthNum union all
select 7 as MonthNum union all
select 8 as MonthNum union all
select 9 as MonthNum union all
select 10 as MonthNum union all
select 11 as MonthNum union all
select 12 as MonthNum)
select
MonthNum,
DATENAME(MONTH, DATEADD(mm, MonthNum - 1, cast('19000101' as datetime)))
from
MonthNumbers;
If you want to guarantee the order, do the sort in memory on the client using the MonthNum.
They are about 6 one-half dozen the other with regard to speed. They have totally different execution plans, however.
January 17, 2011 at 4:04 am
If you're doing this in ASP.NET why go to the database at all, why not do it in the page itself?
That would be the more usual way of doing this, especially if you wanted to display the month names in the user's own language, rather than whatever language the database is using (which might well differ).
January 18, 2011 at 9:14 am
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply