Blog Stats
  • Posts - 12
  • Articles - 0
  • Comments - 115
  • Trackbacks - 0

 

Formatting number to add leading zeros - SQL Server

Formatting numbers to add leading zeros can be done in SQL Server. It is just simple. Lets create a new table and see how it works:

CREATE TABLE Numbers(Num INT);

Table Created.

Lets insert few values and see:

    INSERT Numbers VALUES('12');
    INSERT Numbers VALUES('112');
    INSERT Numbers VALUES('12');
    INSERT Numbers VALUES('122');
    INSERT Numbers VALUES('122');

1 row(s) affected.
1 row(s) affected.
1 row(s) affected.
1 row(s) affected.
1 row(s) affected.

Now we can see how the numbers are formatted with 6 digits, if it has less than 6 digits it will add leading zeros.
Data:

SELECT * FROM Numbers;

Num
12
112
12
122
122

5 row(s) affected.

Formatting:
SELECT RIGHT('00000'+ CONVERT(VARCHAR,Num),6) AS NUM FROM Numbers;

NUM
000012
000112
000012
000122
000122

5 row(s) affected.

Feedback

# re: Formatting number to add leading zeros - SQL Server

Gravatar Sweet ! Thanks for the tip. 7/10/2009 10:33 AM | Craig

# re: Formatting number to add leading zeros - SQL Server

Gravatar thanks a lot, good one 7/27/2009 12:32 PM | dinal

# re: Formatting number to add leading zeros - SQL Server

Gravatar Very, very clever! 10/6/2009 12:52 PM | Nancy

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks for the tip 10/8/2009 2:39 PM | Gregorio Reyes

# re: Formatting number to add leading zeros - SQL Server

Gravatar Cool, and for negative numbers we have:
SELECT RIGHT('00000'+ CONVERT(VARCHAR,-2),6);

0000-2

Really COOL! 11/20/2009 10:52 AM | yarick

# re: Formatting number to add leading zeros - SQL Server

Gravatar Awesome little solution. Simple and effective. 12/3/2009 4:34 PM | Sandy

# re: Formatting number to add leading zeros - SQL Server

Gravatar thank you 12/30/2009 11:11 PM | Nam Nguyen Thanh

# re: Formatting number to add leading zeros - SQL Server

Gravatar It saved ton of my time. Thanks 1/11/2010 5:14 PM | kk

# re: Formatting number to add leading zeros - SQL Server

Gravatar thanks for the Code.

Very Good .
-Arun
Shrinivas Technologies 2/15/2010 10:07 PM | Arun B M

# re: Formatting number to add leading zeros - SQL Server

Gravatar wonderful tip 3/4/2010 11:26 AM | gautham

# re: Formatting number to add leading zeros - SQL Server

Gravatar this is sooo sweet tips........... 3/23/2010 8:16 PM | Shohel

# re: Formatting number to add leading zeros - SQL Server

Gravatar Your article is really very interesting. This is the information, I’ve been looking for… Thanks
BUY AMBIEN 4/26/2010 12:24 PM | Kate

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks alot. 5/4/2010 7:04 AM | Giri

# re: Formatting number to add leading zeros - SQL Server

Gravatar Little.. but great !! Very helpful after searching lots of sites for solutions... thanks 5/9/2010 12:06 PM | JigneSH

# re: Formatting number to add leading zeros - SQL Server

Gravatar Awesome application of mind...

Gr8...

Thanks 6/1/2010 8:23 AM | Braj

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks !

Helpful ! 6/25/2010 4:59 AM | Narinder Jit Singh

# re: Formatting number to add leading zeros - SQL Server

Gravatar Wow, Genius, Intelligent, Thanks A lot! 7/12/2010 12:05 AM | waasim

# re: Formatting number to add leading zeros - SQL Server

Gravatar great solution, thx 7/19/2010 7:46 AM | W0333y

# re: Formatting number to add leading zeros - SQL Server

Gravatar thank you so much 7/26/2010 6:30 AM | lakshmipathy

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thx for the instrucruion. Look if I've done right or not here СДЕЛАТЬ САЙТ ПЕТРОЗАВОДСК 10/23/2010 7:54 AM | lurry

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks mate, is really very clever!! 11/4/2010 5:36 AM | Marcio

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks a bunch - saved me time figuring it out :) 11/24/2010 1:37 PM | Deb

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks,,
Nice . i search lot of website . but i get the solution here only. thaks alot 12/10/2010 2:43 AM | Ravindran

# re: Formatting number to add leading zeros - SQL Server

Gravatar Check out the value of NUM with more than 6 digit

-- Its dangerous :-) 12/14/2010 5:47 AM | Kiran

# re: Formatting number to add leading zeros - SQL Server

Gravatar wow, that is useful info!
Buy generic paxil Online
buy cheap Zyrtec 12/14/2010 7:23 PM | Meyidentity

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks for this, but I have another question regarding this :

My database fields looks like :
1.00000000 (a 1 with 8 decimals) which I want to convert and export to:
000001.000

So 10 character with always 3 decimals.

Can somebody help me with that? 1/18/2011 12:28 PM | Joost van der Meer

# re: Formatting number to add leading zeros - SQL Server

Gravatar this is very help full for me
Thanks a lots 2/10/2011 5:25 AM | Rajdeep

# re: Formatting number to add leading zeros - SQL Server

Gravatar This is an awesome tip! Thank you so very much for sharing it! 2/24/2011 11:30 AM | Jaes W Overley

# Good one

Gravatar Smooth. Big help. Thanks!

FYI I'm working in ColdFusion and had issues w/a query returning anything with leading zeros into an AJAX binded object; cfselect in my case. It passes the query as JSON which automatically drops leading zeros. I forced a linefeed character, #Chr(10)#, in the select to make it work. Dirty, but effective. :)

Happy coding! 2/25/2011 12:59 PM | John M

# re: Formatting number to add leading zeros - SQL Server

Gravatar good, you helped me.... thanks. 3/3/2011 3:57 AM | vvv

# re: Formatting number to add leading zeros - SQL Server

Gravatar really thank dear.
3/23/2011 8:24 AM | ganpat

# re: Formatting number to add leading zeros - SQL Server

Gravatar You are my hero. I wish I'd thought of that! 3/31/2011 1:51 PM | Michael

# re: Formatting number to add leading zeros - SQL Server

Gravatar Can someting similar be done with a char field? 4/1/2011 12:29 AM | Nate

# re: Formatting number to add leading zeros - SQL Server

Gravatar Great solution! 4/5/2011 9:04 AM | Esteban

# re: Formatting number to add leading zeros - SQL Server

Gravatar My problem is slightly different - I join two systems in personid - one system uses leading zeros , the other doesn't. So persin with ID 0001 in system A is matched with person with ID 1 in system B leading to a cartenian product. Any suggestions? 4/8/2011 4:48 AM | Alison

# re: Formatting number to add leading zeros - SQL Server

Gravatar Great tip, thanks for posting. 6/10/2011 2:18 AM | Azonic

# re: Formatting number to add leading zeros - SQL Server

Gravatar Simple but bailed me out today - thanks! 7/12/2011 1:13 AM | hwallbanger

# re: Formatting number to add leading zeros - SQL Server

Gravatar This doesnt work, as the results would be :
0000012
00000112
0000012
00000122
00000122

your just concatenating the string '00000' with the number ¬¬ 7/14/2011 11:23 AM | Eder

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks man. But how do I do it in access ?? 8/1/2011 3:39 AM | Santosh

# re: Formatting number to add leading zeros - SQL Server

Gravatar Crap, only works with positive numbers 8/11/2011 9:57 PM | Fred Flintstone

# re: Formatting number to add leading zeros - SQL Server

Gravatar Perfect, exactly what I was looking for.
Thanks. 8/30/2011 4:00 AM | jim

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thank you very much 10/2/2011 5:49 PM | ALEX

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thank you very much for your help, cheers :) 10/2/2011 5:49 PM | ALEX

# re: Formatting number to add leading zeros - SQL Server

Gravatar That worked perfectly. 10/4/2011 2:04 AM | Mike Klaarhamer

# re: Formatting number to add leading zeros - SQL Server

Gravatar hah, very clever. thanks! 10/13/2011 5:55 AM | chuck

# re: Formatting number to add leading zeros - SQL Server

Gravatar Muy bueno, gracias. 11/5/2011 3:32 AM | Dani

# re: Formatting number to add leading zeros - SQL Server

Gravatar Hi,

This is a great idea to solve this situation.

Thanks,
Chen 11/9/2011 8:02 PM | Chen Noam

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thank you so much for this. 12/13/2011 3:15 AM | Lidia

# re: Formatting number to add leading zeros - SQL Server

Gravatar Awesome, thanks a lot. 1/3/2012 6:19 AM | Aga

# re: Formatting number to add leading zeros - SQL Server

Gravatar thanks for the code 1/7/2012 12:53 PM | Accountants

# re: Formatting number to add leading zeros - SQL Server

Gravatar great! works good 1/7/2012 12:54 PM | Accountants

# re: Formatting number to add leading zeros - SQL Server

Gravatar thanks again 1/7/2012 12:55 PM | Accountants

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks for sharing 1/29/2012 7:28 PM | Chandralekha

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks a lot. Nice trick. 2/8/2012 5:10 PM | Don Smith

# re: Formatting number to add leading zeros - SQL Server

Gravatar too tricky 2/10/2012 7:59 PM | VIJAY

# re: Formatting number to add leading zeros - SQL Server

Gravatar Nice solution 4/16/2012 8:49 PM | Vijay

# re: Formatting number to add leading zeros - SQL Server

Gravatar Very nice solution 4/19/2012 7:09 PM | Kanagasabai

# re: Formatting number to add leading zeros - SQL Server

Gravatar Thanks!! it was simple , smart solution thanks 4/30/2012 10:47 PM | Anthony

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © nagendraprasad