If you are on this page, you must have searched how to Insert Leading zeroes in your MSSQL queries. Or maybe any SQL platform. For those not in the know, here’s the simple scenario.
Simple SQL Select Query
select TotalAmount from BurgerValue
Results
TotalAmount
-----------
10.25
6572.46
From the above, this is as per expected, but what if you wanted to extract out the data later with leading zeroes in their value. You can do it two ways, let the SQL platform create the zeroes first or add in later using code in your desired platform.
What are the expected Leading Zeroes?
How about the expected result as per below insert leading zeroes and how to achieve it?
Desired Result
TotalAmount
-----------
0000010.25
0006572.46
Since this post is about creating one on SQL, let’s only focus on this.
MSSQL Insert Leading Zeroes
Some reasons why the above is needed is due to request from users in order to feed other systems based on value to avoid calculation mismatch. Others are due to restrictions from legacy systems which needs certain values.
select RIGHT(CONCAT('0000000', TotalAmount), 10) from BurgerValue
There you have it guys, the above results will generate as per expected results. There are many other ways to write the SQL but I find the above is suitable enough as it can also work with numeric values. There are some which tried to convert the values into String before adding the 0s (zeroes) but the above is all I ever need.
Feel free to comment.
SHOUT OUT
Also thank you to a reader David who has donated by PayPal to this site.
Your contribution has helped to motivate and also to note that this site is still relevant to you. A YouTube channel is still under consideration too, as we embraced the new change to the modern way of acquiring knowledge. If you would like to donate for the help received, please click on the button on the right and donate to our site or you can donate any amount you wanted by going here