Convert Numeric to Character

crjunk picture crjunk · Jul 8, 2011 · Viewed 31.3k times · Source

I'm not very familiar with FoxPro and I'm running into problems trying to modify a report. I have a numeric value that I want to convert to text so that I can get the first four characters, but I am only able to get the first two characters.

When I execute the following in the command window: ? LEFT(STR(20110547),4)

The following value is displayed: 20

I expected it to return 2011. Am I doing something wrong or is there something that I am not accounting for?

Thanks, C.R.

Answer

DaveB picture DaveB · Jul 8, 2011

There are a couple of ways to do this:

LEFT(ALLTRIM(STR(20110547)),4)

You must LTRIM or ALLTRIM the STR function as it adds leading spaces. From the documentation:

STR( ) pads the character string it returns with leading spaces if you specify a length larger than the number of digits to the left of the decimal point. STR( ) returns a string of asterisks, indicating numeric overflow, if you specify a length less than the number of digits to the left of the decimal point.

You could also use the TRANSFORM function:

LEFT(transform(20110547),4)