14,097,639 members
See more:
I have a table name updates property is Date,Jan,feb,march,april...dec

That means that i have the Date column and 12 months

The question is that i have to select 3 months on the bases of the date. Suppose the if the Date is 1 it should select the current month and last 2 months. That is for from date 1 to 31 it should display the results for the current months and last two months.
so question is how can the months could be selected automatically.

My current query is
```SqlCommand command = new SqlCommand("SELECT Date_,Decm,Jan,Feb from updates order by Date_", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();```
Posted
Updated 4-Feb-16 7:24am
v3

## Solution 1

Declare a function which returns current month name with next two months:
```public static string GetMonths(int month)
{
string[] monthColumns = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

string monthName = string.Empty;

//// Get current month with next two months
//monthName = monthColumns[month - 1] + ", "
//        + monthColumns[(12 + (((month) > 11) ? 0 : month)) - 12] + ", "
//        + monthColumns[(12 + (((month + 1) > 11) ? (month + 1) - 12 : month + 1)) - 12];

// Get current month with previous month
monthName = monthColumns[((month - 3) < 0) ? (12 + (month - 3)) : month - 3] + ", "
+ monthColumns[((month - 2) < 0) ? (12 + (month - 2)) : month - 2] + ", "
+ monthColumns[month-1];

return monthName;
}```

Next call the above method:
```string sqlQuery = "SELECT Date_, "+ GetMonths(DateTime.Now.Month)+ " FROM updates order by Date_";

// Next execute above sqlQuery as per your code
SqlCommand command = new SqlCommand(sqlQuery, con);
v3
Shantanu sinha 3-Feb-16 14:40pm

the solution is fine but its giving result for current month +next two months
where as i want last two month + current months.
Manas_Kumar 3-Feb-16 21:43pm

Can you give 2-3 cases here..
Shantanu sinha 4-Feb-16 11:37am

suppose you have to display today result then the display should be like:-
Date_ Dec Jan Fab
1 23 32 09
2 55 33 45
3 32 23 55
4 45 43 01
5 62 02
6 12 11
7 24 12
8 12 32
9 21 63
10 76 32
11 22 32
.
.
31 21 32
and if the month is march
then result should be like:-
Date_ Jan Feb Mar
1
2
3
4
5
6
7
Manas_Kumar 4-Feb-16 12:46pm

If it current month is Jan or Feb then what result do you expect?

Shantanu sinha 19-Feb-16 7:33am

the updated one is fine
but what i am looking is
if suppose current month is Feb
then result should be
Dec Jan Feb
if current month is march
then the result should be
jan feb March
if current month is December
then the result should be
Oct Nov Dec

Top Experts
Last 24hrsThis month
 OriginalGriff 221 Gerry Schmitz 120 Richard MacCutchan 100 Thomas Daniels 80 CHill60 70
 OriginalGriff 3,796 Richard MacCutchan 1,635 Patrice T 1,137 Maciej Los 908 Gerry Schmitz 880