Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi,
i want to convert number to month
but dont know how to do ?
currently i have this output from storedprocedure
Month
========
04
05
01
02
11
10

but i want this output without changes storedprocedure query coz its used at other pages so cant change sp.
Month
=======
April
May
January
February
November
December


Please give me a solution
thanx
Posted
Comments
Sergey Alexandrovich Kryukov 27-Apr-15 0:12am    
What have you tried so far?
—SA
PIEBALDconsult 27-Apr-15 0:13am    
Bad idea. Consider globalization.
Naveen Kumar Tiwari 27-Apr-15 6:22am    
Make a Scaler Valued SQL function and call this function in SP which will convert number to month......
Naveen Kumar Tiwari 27-Apr-15 6:24am    
I think you have to tag SQL server in place of C#...

Try to merge this query into your existing query where the month is coming.

This query will convert number of month to name of month.

Now there is no need to conversion on codebehind page, no code required.


Select DateName( month , DateAdd( month , @MonthNumber , -1 ) )
 
Share this answer
 
hi .
you can use enum.
define enum:
C#
public enum Month
        {
            April = 4,
            May = 5
        }

and save enum values into database then if you want to read month from your database
you can use Reflection or cast saved values to enum correspinding values.
i hope this help you
 
Share this answer
 
v2
Please, read comments to the question.

Let's say, your stored procedure returns list of strings ("04", "05", etc.). So, you can use Linq to "convert" it into month names:
C#
List<string> months = new List<string>(){"04", "05", "01", "02", "11", "10"};

var qry = months.Select(a=>new{myMonth = (new DateTime(2015, Convert.ToInt32(a),1)).ToString("MMMM")});
foreach(var m in qry)
{
    Console.WriteLine("{0}", m);
}
 
Share this answer
 
Hi,

C#
var monthinNumber = new List<int>() { 04, 05, 01, 02, 11, 10 };
               var monthNames = monthinNumber.Select(item => CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(item)).ToList();


thanks
Bimal
 
Share this answer
 
v2
I have tested this function...If your requirement is same as i understood then
this function will help you for sure..
Call this function in SP and pass parameter in this function whatever you want..
Parameter Data type must be int...
All of these Which one find your Requirement use that...
SQL Function is below


SQL
CREATE FUNCTION [dbo].[Demo1]
(
    @MonthNumber int
)
RETURNS varchar(8)
AS
BEGIN
    Declare @RDate varchar(max)
    Declare @NDate varchar(max)
    set @RDate= (@Date)
    IF(@Date<> 0)
        SET @NDate=SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@RDate * 4) - 3, 3)
    ELSE
        SET @NDate='-'

    RETURN @NDate

END


OR You Can also use this way

Select DateName( month , DateAdd( month , @MonthNumber , 0 ) - 1 )
OR
Select DateName( month , DateAdd( month , @MonthNumber , -1 ) )

Thanks
NK___
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900