Solved

Add 1 zero in front of a number if 5 digits but if 6 digits stay as it is

Posted on 2016-08-31
4
43 Views
Last Modified: 2016-08-31
I want to add 1 zero in front of 5 digit numbers but no need to add if there are 6 digit numbers in  VB.net 2010.


Example:

12258 into 012258
400488 into 400488

Thank you for your help.
0
Comment
Question by:Queennie L
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41778611
The SQL Server T-SQL answer would be...
SELECT RIGHT('00000' + CAST(your_number as varchar(10)), 6) 

Open in new window

This requires converting the number to a character data type, as numbers do not support leading zeros.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41778620
if you want to do it in VB, you can use PadLeft (https://msdn.microsoft.com/en-us/library/92h5dc07(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2):
dim x as string = "12258"
x = x.PadLeft(6, "0")

Open in new window

0
 

Author Closing Comment

by:Queennie L
ID: 41778641
Thank you. It worked.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 41778660
Expanding on what Eric has stated, another way, if you are dealing with numerics specifically, is to use the ToString method with a custom format specified; e.g. -
Module Module1
	Sub Main()
		Dim values = {1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789}
		Console.WriteLine("Padding zeros -")
		For Each value In values
			Console.WriteLine(value.ToString(String.Format("D{0}", values.Max().ToString().Length())))
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output -Capture.JPGSo for your specific case you could simply use:
Dim value = 12258
Console.WriteLine(value.ToString("D6"))
'' Output will be
'' 012258

Open in new window


Proof of concept -
Module Module1
	Sub Main()
		Dim value = 12258
		Console.WriteLine(value.ToString("D6"))
		Console.ReadLine()
	End Sub
End Module

Open in new window

Output -Capture.JPG
-saige-
1

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question