Kemin Zhou wrote:
Here I have a table column defined as integer type.

it stores number from 1 to the hundred range (3 digits).

For nice output (without using any external programming languages),
I would want the printed type to have zerofill.

One way to do it is to convert the type of the column to the zerofill.
Is there another way to do it?  Such as round, cast.

Kemin

I think LPAD(string, length, pad_string) is what you want. MySQL will automatically convert your integer to a string if used in string context, so something like this should do:

  SELECT LPAD(int_col, 3, '0') FROM your_table;

For example:

  mysql> SELECT LPAD(13, 3, '0');
  +------------------+
  | LPAD(13, 3, '0') |
  +------------------+
  | 013              |
  +------------------+
  1 row in set (0.00 sec)

LPAD() is described on the string functions page in the manual <http://dev.mysql.com/doc/mysql/en/string-functions.html>. One caveat: if the input string is longer than the given length, the string gets truncated (on the right).

  mysql> SELECT LPAD(1234, 3, '0');
  +--------------------+
  | LPAD(1234, 3, '0') |
  +--------------------+
  | 123                |
  +--------------------+
  1 row in set (0.00 sec)

Michael

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to