DEV Community

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

Posted on

3 2

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay