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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay