DEV Community

Cover image for MariaDB Quick-tip #3 - Prepending zeros
Allan Simonsen
Allan Simonsen

Posted on

MariaDB Quick-tip #3 - Prepending zeros

MariaDB tips and tricks

This is part of a series of quick tips and tricks I have accumulated over the year, that I think can be useful for others.
If you have similar short tips and tricks please leave a comment.

Prepending zeros

Some data formats require that your numbers have prepended zeros so you can get the MariaDb to format the number in your query. This is done by using the LPAD(str, len [, padstr]) function and it is alot more elegant than what you have to do to achieve the same result on the SQL Server.

CREATE OR REPLACE TEMPORARY TABLE names (Name VARCHAR(50), ImportantNumber INT);

INSERT INTO names (Name, ImportantNumber) 
VALUES ('Joe', 2), ('Bob', 4), ('Anne', 42), ('Jane', 134);

SELECT Name, LPAD(ImportantNumber, 4, 0) AS ImportantNumber
  FROM names
Enter fullscreen mode Exit fullscreen mode

DBeaver screenshot

Discussion (0)