How to convert int into Months

Posted by Raj.Trivedi under Sql Server on 4/3/2013 | Points: 10 | Views : 968 | Status : [Member] [MVP] | Replies : 5
Hello Team.

I have a table in SQL Server where i am storing Months as integer means


MonthID = MonthNames

1 = January
2 = Feburary
3 = March
4 = April
5 = May
6 = June
7 = July
8 = August
9 = September
10 = October
11 = Novemeber
12 = December

I am fetching data from table what i am trying to do is i want to convert 1 = January
i.e. if in dataset the Monthid is 1 then in the grid it should show as January not 1

Regards,
Raj.Trivedi

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved



Responses

Posted by: Satyapriyanayak on: 4/3/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication18._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication18
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "SELECT DateName(mm,DATEADD(mm,MonthNumber,-1)) as [MonthName]FROM t1 where MonthNumber ='" + TextBox1.Text + "' and MonthNumber <= 12";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "t1");
GridView1.DataMember = "t1";
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}
}


If this post helps you mark it as answer
Thanks

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 4/3/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down

hi

try this without db becos performance issue so try this


public enum GetMontNames
{

January = 1, Feburary = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9, October = 10, Novemeber = 11, December = 12
}
protected void bt1_Click(object sender, EventArgs e)
{
string[] values = Enum.GetNames(typeof(GetMontNames));
foreach (string s in values)
{

}

int[] n = (int[])Enum.GetValues(typeof(GetMontNames));
foreach (int z in n)
{

}

}

Mark as Answer if its helpful to you

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandeepmhatre on: 4/11/2013 [Member] Starter | Points: 25

Up
0
Down
i think your data design is not proper ,
you should store date in a table instead of Month No:

By using different sql datetime functions you can manipulate the date as a way you want.

Eg: for the month January you need to store 01-01-2013,

with DATENAME Function in sql, you can retrive the JANUARY as Column value.

DATENAME( MONTH , GETDATE())


Sandeep M,
Software Developer
Follow me on :
http://sandeepmhatre.blogspot.in

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Learningtorise on: 4/16/2013 [Member] Starter | Points: 25

Up
0
Down
USE DateName for this purpose:

Syntax:

DATENAME ( datepart , date )

Example :
SELECT DATENAME(month, GETDATE())




http://hashtagakash.wordpress.com/

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pandians on: 4/17/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Check It Out!
CREATE TABLE MyTable1

(
MonthID INT IDENTITY(1,1)
)
GO

INSERT MyTable1 DEFAULT VALUES
GO 12

SELECT MonthID, DATENAME(Month,'2013-' + CAST(MonthID AS VARCHAR)+ '-01') [MonthName] FROM MyTable1
GO
Result
MonthID	MonthName

------- ---------
1 January
2 February
3 March
4 April
5 May
6 June
7 July
8 August
9 September
10 October
11 November
12 December


Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response