Click here to Skip to main content
15,883,990 members
Articles / Programming Languages / SQL

Pad Numbers with Leading and Ending Zeros

Rate me:
Please Sign up or sign in to vote.
3.03/5 (10 votes)
27 Jul 2016CPOL 14.4K   1   4
Pad numbers with leading and ending zeros

In a recent project, I got a list of integers something similar to the following:

1
12
123
1234
12345
….
123456789

and, I was asked to add zeros to the left or right of the list and make it as below:

List 1:
0000000001
0000000012
0000000123
…..
0123456789

List 2:
1000000000
1200000000
1230000000
….
1234567890

C# – PadLeft / PadRight

In C#, you can use PadLeft and PadRight functions to do this:

String.PadLeft Method (Int32, Char)<br />
String.PadRight Method (Int32, Char)

C#
int[] numbers = { 1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789 };

Console.WriteLine("List 1");
foreach (var i in numbers)
{
Console.WriteLine(i.ToString().PadLeft(10, '0'));
}

Console.WriteLine();
Console.WriteLine("List 2");
foreach (var i in numbers)
{
Console.WriteLine(i.ToString().PadRight(10, '0'));
}

PadLeft and PadRight

PadLeft and PadRight

SQL

Here comes the interesting part, I had to do this using SQL. In SQL, there are no inbuilt PadLeft or PadRight functions. So, I have used the LEFT and RIGHT SQL functions with a small hack.

SQL
CREATE TABLE [dbo].[NumberPadding](
[Number] [int] NOT NULL
) ON [PRIMARY]
GO;

INSERT INTO dbo.NumberPadding
VALUES (1), (12), (123), (1234), (12345), (123456), (1234567), (12345678), (123456789)
GO;

SELECT LEFT (CAST([Number] AS VARCHAR) + '0000000000', 10) FROM dbo.NumberPadding;

SELECT RIGHT ('0000000000' + CAST([Number] AS VARCHAR) , 10) FROM dbo.NumberPadding;

SQL - LEFT, RIGHT FUNCTIONS

SQL – LEFT, RIGHT FUNCTIONS

Filed under: C#, CodeProject, SQL
Tagged: C#, Database, internet, software, SQL 2005, SQL 2008, SQL Server, technology, Web
Image 3 Image 4

This article was originally posted at https://tharakaweb.com/2016/07/24/small-exercise

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Eyepax IT Consulting (Pvt) Ltd.
Sri Lanka Sri Lanka
Having more than 9 year hands-on industry experience in software development
Responsible for designing, implementing and managing complex software systems with stringent up-time requirement.

Visit my blog

Comments and Discussions

 
GeneralThoughts Pin
PIEBALDconsult26-Jul-16 15:36
mvePIEBALDconsult26-Jul-16 15:36 
GeneralRe: Thoughts Pin
Tharaka MTR26-Jul-16 23:20
professionalTharaka MTR26-Jul-16 23:20 
GeneralMy vote of 4 Pin
@k@ ?25-Jul-16 14:24
professional@k@ ?25-Jul-16 14:24 
GeneralRe: My vote of 4 Pin
Tharaka MTR26-Jul-16 2:39
professionalTharaka MTR26-Jul-16 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.