55 pts.
 I want to select months from Jan to Dec using SQL query
I want to select months from January to December using SQL query. I will use this for list item note:- without using PL/SQL and less union.

Software/Hardware used:
ASKED: May 4, 2009  9:04 AM
UPDATED: April 19, 2013  7:08 PM
  Help
 Approved Answer - Chosen by Kccrosser

Hi guys !! =D I think I can finally can collaborate in a good manner =D. Check out this one:

SELECT TOP 12
DATENAME(MONTH, DATEADD(MONTH,ROW_NUMBER() OVER (ORDER BY object_id) - 1,0))
FROM sys.columns

Do you like it?

ANSWERED:  Mar 8, 2011  2:06 PM (GMT)  by Kccrosser

 
Other Answers:

I think Cyberloco’s answer meets the requirements. Deleting my suggested approach.

Last Wiki Answer Submitted:  April 19, 2013  7:08 pm  by  Kccrosser   3,830 pts.
Latest Answer Wiki Contributors:  Michael Tidmarsh   11,410 pts. , Kccrosser   3,830 pts. , carlosdl   63,535 pts.
To see other answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Some additional information on your data structure and requirements would help.

Assuming your data is in a table with a date and a value, e.g.:

table myTable (
theDate date,
theValue integer);

and you want to generate a result set with the sum of the values that fall within the given months, you could use:

select
jandata.total "Jan", febdata.total "Feb", mardata.total "Mar", aprdata.total "Apr", ...
from
(select sum(theValue) total from myTable where theDate >= '01/01/2009' and theDate < '02/01/2009') jandata,
(select sum(theValue) total from myTable where theDate >= '02/01/2009' and theDate < '03/01/2009') febdata,
(select sum(theValue) total from myTable where theDate >= '03/01/2009' and theDate < '04/01/2009') mardata,
...

This is brute force, but doesn’t require any PL/SQL or unions.

(Above is in Transact-SQL, in Oracle you would need to cast the dates using the “to_date” function.)

 3,830 pts.

 

I guess he wants to select the month names to populate a list box.

january
february
march

 63,535 pts.

 

Bizarre – why no union, which would make that trivial?

With those constraints, I think you are correct that the only solution is a table of month names.

 3,830 pts.

 

From database table you access months in Data Table object
by using query

" SELECT * FROM  tableMonth"

then catch in DataTable object of  ADO.net

public DataTable SelectPeron(int Pid)
{
DataTable tab = new DataTable();
string Con = @"Data Source=sureit44;Initial Catalog=MydataBase;Persist Security Info=True;User ID=sitdev;Password=sitdev1";
using (SqlConnection con = new SqlConnection(Con))
{
string query="Select * FROM tableMonth";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();

using (SqlDataAdapter da = new SqlDataAdapter(cmd))
da.Fill(tab);
con.Close();

}
return tab;

After that Bind the tab object With DropDownList

ddl.DataSource=tab;
ddl.DataBind();

I think this will help you  and  u will get  your  d -zire result
Thanks
 145 pts.

 

> without using pl/sql

t-sql

>and less union

one union
^-))

with mth as(
select 1 as mnum
union all 
select 1 + mnum from mth where mnum<12)
select mnum, datename(month,DATEADD(month,mnum-1,'010101'))  from mth;
 1,610 pts.

 

I just realized that it has similatiry with the one provided by Mr. Msi777.

 40 pts.

 

Good job, Cyberloco ;-)

 63,535 pts.