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


This content originally appeared on DEV Community and was authored by Allan Simonsen

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

DBeaver screenshot


This content originally appeared on DEV Community and was authored by Allan Simonsen


Print Share Comment Cite Upload Translate Updates
APA

Allan Simonsen | Sciencx (2022-07-05T02:43:49+00:00) MariaDB Quick-tip #3 – Prepending zeros. Retrieved from https://www.scien.cx/2022/07/05/mariadb-quick-tip-3-prepending-zeros/

MLA
" » MariaDB Quick-tip #3 – Prepending zeros." Allan Simonsen | Sciencx - Tuesday July 5, 2022, https://www.scien.cx/2022/07/05/mariadb-quick-tip-3-prepending-zeros/
HARVARD
Allan Simonsen | Sciencx Tuesday July 5, 2022 » MariaDB Quick-tip #3 – Prepending zeros., viewed ,<https://www.scien.cx/2022/07/05/mariadb-quick-tip-3-prepending-zeros/>
VANCOUVER
Allan Simonsen | Sciencx - » MariaDB Quick-tip #3 – Prepending zeros. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/05/mariadb-quick-tip-3-prepending-zeros/
CHICAGO
" » MariaDB Quick-tip #3 – Prepending zeros." Allan Simonsen | Sciencx - Accessed . https://www.scien.cx/2022/07/05/mariadb-quick-tip-3-prepending-zeros/
IEEE
" » MariaDB Quick-tip #3 – Prepending zeros." Allan Simonsen | Sciencx [Online]. Available: https://www.scien.cx/2022/07/05/mariadb-quick-tip-3-prepending-zeros/. [Accessed: ]
rf:citation
» MariaDB Quick-tip #3 – Prepending zeros | Allan Simonsen | Sciencx | https://www.scien.cx/2022/07/05/mariadb-quick-tip-3-prepending-zeros/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.