Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert month into number 1

Status
Not open for further replies.

alisaif

ISP
Apr 6, 2013
418
AE
Hi,

How can I convert month in text into number (numeric) like

January
1
February
2
..
..
..
December
12

Thanks

Saif
 
see MONTH()
also see YEAR(), DAY(), HOUR(), MINUTE(), and SEC(). We have functions to get any part of a date or datetime value.

Bye, Olaf.
 
Sorry, misunderstood.

If you have the text "December", then CTOD() can help, if you don't have just the month name, but a long date format.

A simple solution to lookup the month number by a month name is to create a lookup table with (name, monthnumber), index on name and seek it or use LOOKUP(). There is no function turning month names to month numbers.

Bye, Olaf.
 
Olaf is correct. Make a lookup table.

However, a table with only 12 entries does not need to be indexed as you will not notice any difference in speed using LOCATE versus SEEK.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Just to add to the above solutions -

* Declare the variables as required, i.e. public, private or local
Cx_MonthNames = upper("Xyz,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec")

* Then, to convert MonthNumber to MonthName
i_MonthNumber = at(','+left(upper(MonthName),3),cx_MonthNames) / 4 && add comma(,) to cut the error


* Further, to convert MonthNumber to MonthName
MonthName = substr(cx_MonthNames,MonthNumber*4+1,3)


nasib



 
What about a simple case statement...

cMonthName = upper(left(alltrim(cMonthName),3))

do case
case cMonthName = "JAN"
nMonthNumber = 1
case cMonthName = "FEB"
nMonthNumber = 2
case cMonthName = "MAR"
nMonthNumber = 3
case cMonthName = "APR"
nMonthNumber = 4
.........
endcase

By using a case statement, you can tweak it to whatever. Here I defined it exactly as 3 character uppercase months...
Stanley
 
Or, yet another possibility:

Code:
lcValueToTest = <the month name to test>

DIMENSION MonthNames(12)
MonthNames(1) = "January"
MonthNames(2) = "February"
MonthNames(3) = "March"
MonthNames(4) = "April"
MonthNames(5) = "May"
MonthNames(6) = "June"
MonthNames(7) = "July"
MonthNames(8) = "August"
MonthNames(9) = "September"
MonthNames(10) = "October"
MonthNames(11) = "November"
MonthNames(12) = "December"

lnMonthNumber = ASCAN(MonthNames, lcValueToTest, -1, -1, 1)

All the suggestions you have received are equally valid. Just choose the one you like the best. Some will be faster than others, but unles you are doing this in a loop that's executed thousands of times, a difference in performance is not likely to be noticeable.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks for the reply!

I am using stanlyn solution:

cMonthName = upper(left(alltrim(cMonthName),3))
do case
case cMonthName = "JAN"
nMonthNumber = 1
case cMonthName = "FEB"
nMonthNumber = 2
case cMonthName = "MAR"
nMonthNumber = 3
case cMonthName = "APR"
nMonthNumber = 4
.........
endcase

Thanks

Saif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top