1

I have a column called Month in a stored proc which returns to me the month in string name terms (eg. January, Febuary, March etc.)

I would like to convert that to an integer.

Is that possible without just using a number of select case statements?

I would like to do this in .NET 3.5

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
Diskdrive
  • 18,107
  • 27
  • 101
  • 167
  • possible duplicate of [How to parse a month name (string) to an integer for comparison in C#?](http://stackoverflow.com/questions/258793/how-to-parse-a-month-name-string-to-an-integer-for-comparison-in-c) – Diskdrive Aug 06 '10 at 02:07
  • http://stackoverflow.com/questions/258793/how-to-parse-a-month-name-string-to-an-integer-for-comparison-in-c/258828#258828 – Necros Aug 05 '10 at 01:53

4 Answers4

2

You can use DateTime.Parse and then get the Month property of the date:

string monthName = "January";
DateTime.Parse(String.Format("{0} 1, 2000", monthName)).Month
Tahbaza
  • 9,486
  • 2
  • 26
  • 39
0

I came through this issue recently (on Client Side using Javascript) and after a few searches on the internet, I decided to do it myself. Though I did it using Javascript, still I'll give u a .net solution as well. The Javascript first:

function convertToIntMonth(monText) {
//Your arrays for the valid months for which you can throw exception (I did not do it) if the parameter is out of these 12.
        var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        var monInt;
        for (i = 0; i < 12; i++) {
//in this loop, we'll check for the match in parameter and array items.
            if (monText == monthNames[i])
            { 
//If match is found, we'll return the +1 value of actual match (as month array's index goes from 0 to 11, and we require it from 1 to 12)
monInt=i+1}
        }
        return monInt;
    }

Now the .NET (C#): Did not tested yet, still I hope you could find the logic

public int convertToIntMonth(string monText) {
        string[] monthNames = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        string monInt=0;
        for (int i = 0; i < 12; i++) {
            if (monText == monthNames[i])
            { monInt=i+1}
        }
        return monInt;
    }
Cyberpks
  • 1,401
  • 6
  • 21
  • 51
0

In SQL Server do this SELECT DATEPART(month, GETDATE())

Also a duplicate of this question How to parse a month name (string) to an integer for comparison in C#?

Community
  • 1
  • 1
Ryk
  • 3,072
  • 5
  • 27
  • 32
-1

You could store Name->Number mappings in an array (look through the array until you find the one you want) or a hash (lookup the month name in the hash to get a number -- benefits from supporting any number of languages and abbreviations, too). I'd rather store the information in data structures than in code, but that might just be my own irrational behavior.

EDIT

In response to @roboshop's "Sounds like a lot of work", perhaps it is compared to one-liners that the platform supports, but if your customers ask you to support abbreviations such as "Jan", "Fe", "Mar", I think using a data structure gives a lot of additional flexibility:

months = { "ja" => 1, "jan" => 1, "january" => 1,
           "fe" => 2, "feb" => 2, "february" => 2,
           "ma" => 3, "mar" => 3, "march" => 3,
           "ap" => 4, "apr" => 4, "april" => 4,
           "my" => 5, "may" => 5,
           "jn" => 6, "jun" => 6, "june" => 6,
           "jl" => 7, "jul" => 7, "july" => 7,
           "au" => 8, "aug" => 8, "august" => 8,
           "se" => 9, "sep" => 9, "sept" => 9, "september" => 9,
           "oc" => 10, "oct" => 10, "october" => 10,
           "no" => 11, "nov" => 11, "november" => 11,
           "de" => 12, "dec" => 12, "december" => 12,
           "januar" => 1,
           "februar" => 2,
           "märz" => 3,
           "mai" => 5,
           "juni" => 6,
           "oktober" => 10,
           "dezember" => 12}

puts "Month %s is %d" % [ARGV[0], months[ARGV[0].downcase]]

Of course, now you have to know the localized versions of months your users want, but it is far easier to support requested abbreviations.

sarnold
  • 102,305
  • 22
  • 181
  • 238