CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2006
    Posts
    14

    How to remove leading Zeros

    How to remove leading Zeros in the nvarchar datatype. I would like to do this using C#.

    Example:

    0001234
    0000001234
    00001234

    Thanks in Advance...

    Regards,
    R.Radhakrishnan.

  2. #2
    Join Date
    May 2007
    Location
    London
    Posts
    20

    Re: How to remove leading Zeros

    Hi Rad,

    Try something like this:

    Code:
    string nvarchar = "0001234";
    nvarchar = nvarchar.Replace("0", "");
    

  3. #3
    Join Date
    Jun 2006
    Location
    Erlangen, Germany
    Posts
    57

    Re: How to remove leading Zeros

    Just cast to integer and cast again to string ;-)

    I think it should work with:
    Code:
                string sValue = "000123";
                int iTemp = Convert.ToInt32(sValue);
                sValue = Convert.ToString(iTemp);

  4. #4
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to remove leading Zeros

    Quote Originally Posted by Consola
    Hi Rad,

    Try something like this:

    Code:
    string nvarchar = "0001234";
    nvarchar = nvarchar.Replace("0", "");
    
    How do you think this will work if the number contains a 0 anywhere but not in the beginning?

    000202 will become 22.

    Not a solution.

    Laitinen

  5. #5
    Join Date
    May 2007
    Location
    London
    Posts
    20

    Re: How to remove leading Zeros

    So true! As soon as Hubibi posted I realised that it was the leading zeros you wanted removed and not all zeros.

    Use Hubibi's solution. That will work just fine

  6. #6
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to remove leading Zeros

    Try this:

    Code:
    char[] charsToTrim = { '0'};
    string nvarchar = "000012340";
    string newString = nvarchar.TrimStart(charsToTrim);
    Laitinen

  7. #7
    Join Date
    Aug 2006
    Posts
    14

    Thumbs up Re: How to remove leading Zeros

    Hi Laitinen,

    Thanks a lot for your response...

    Best regards,
    Radhakrishnan

  8. #8
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to remove leading Zeros

    Quote Originally Posted by Hubibi
    Just cast to integer and cast again to string ;-)

    I think it should work with:
    Code:
                string sValue = "000123";
                int iTemp = Convert.ToInt32(sValue);
                sValue = Convert.ToString(iTemp);
    Just to point out something that may seem like a detail: This will only work for integers in the range of a Int32. Therefore I think my proposal in post #6 is the way to go.

    Laitinen

  9. #9
    Join Date
    Sep 2010
    Posts
    1

    Re: How to remove leading Zeros

    string paddedString = "000012340";
    string newString = paddedString.TrimStart("0".ToCharArray());

  10. #10
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to remove leading Zeros

    It really depends on how long the NVARCHAR is, as a few of the above (especially the ones that convert through IntXX) methods will not work for:

    Code:
    String s = "005780327584329067506780657065786378061754654532164953264952469215462934562914562194562149516249516294563219437859043758430587066748932647329814687194673219673294677438907385032758065763278963247982360675680570678407806473296472036454612945621946";
    Something like this would:
    Code:
    String s = "0000058757843950000120465875468465874567456745674000004000".TrimStart( new Char[] { '0' } );
    
    // s = "58757843950000120465875468465874567456745674000004000"
    Last edited by rliq; September 15th, 2010 at 12:56 AM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  11. #11
    Join Date
    Sep 2010
    Posts
    23

    Re: How to remove leading Zeros

    string a = "000012340";
    string b = a.TrimStart('0');


    I like things simple, any reason not to remove leading 0's this way?

  12. #12
    Join Date
    Aug 2011
    Posts
    1

    Re: How to remove leading Zeros

    Can someone give me the technical explanation why MySQL would pad "integers" with zeros if the datatype being applied to the value is varchar? (If that makes sense)

    I had a comma separated value file with a column which contains records which were either 100% integer (304923094 for example), but some of the other records were hexadecimal / had characters (35GHQ153, for example). I imported the CSV file into MySQL using Navicat and specified that this column was to be datatype "Varchar 15". The hexadecimal values remained as they were in the CSV file, but the integer values got left padded with 0'sup to 15 total.

    For example, RE122572 would import just like that, but 1258693 would import as 000000001258693

    The left trim method works to fix this, but I want to understand exactly what's going on.

    Thanks!
    Andrew

  13. #13
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How to remove leading Zeros

    Quote Originally Posted by mavy-online View Post
    string a = "000012340";
    string b = a.TrimStart('0');


    I like things simple, any reason not to remove leading 0's this way?
    That is the simplest; as far as I can tell.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to remove leading Zeros

    If they are being padded with 0s it is likely for alpha sorting purposes.

    1 2 3 4 5 6 7 8 9 10 11 ...

    Will sort as 1 10 11 2 3 4 5 6 7 8 9 ...

    With the leading 0s the sort will be in correct numerical order.

    01 02 ...... 10 11
    Always use [code][/code] tags when posting code.

  15. #15
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to remove leading Zeros

    This thread seems to get revived and revived. Please people - NEW MEMBERS - Start a new thread in future.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured