learner
learner

Reputation: 2789

How can I pad a number with zeros in MySQL?

I want to execute this kind of a query:

SELECT 00005

Now its result showing as 5. It not taking '0000'. How to get the correct value. Any body can help me.

Upvotes: 1

Views: 216

Answers (3)

Devart
Devart

Reputation: 122032

You can set a ZEROFILL property, e.g. -

CREATE TABLE table1(
  column1 INT(5) UNSIGNED ZEROFILL DEFAULT NULL
);

SELECT * FROM table1;
+---------+
| column1 |
+---------+
|   00005 |
|   00025 |
+---------+

Upvotes: 1

Eugene Yarmash
Eugene Yarmash

Reputation: 150041

You can use the LPAD function:

SELECT LPAD(5, 5, 0)

Upvotes: 3

Dan Grossman
Dan Grossman

Reputation: 52372

00005 is not a number, but it is a string...

SELECT '00005'

Upvotes: 3

Related Questions