0

I have a varchar value: which takes the following value:

0000151010

from the database

I need this value to show as

151010 (without zeros)
//converting the columns returned to strings.
DBInvoiceModel invoiceRecord = new DBInvoiceModel
{
    //here's the value in the string
    TranNo = reader[1].ToString()
};
result.Add(invoiceRecord);

I need help suppressing the leading zeros

Dale K
  • 25,246
  • 15
  • 42
  • 71
Michele
  • 57
  • 7

2 Answers2

0

Just use Trimstart

TranNo = reader[1].ToString().TrimStart(new Char[] { '0' } );
//Console.WriteLine(TranNo);

//converting the columns returned to strings.
DBInvoiceModel invoiceRecord = new DBInvoiceModel
{
    //here's the value in the string, trimmed out
    TranNo = reader[1].ToString().TrimStart(new Char[] {'0'});
};
result.Add(invoiceRecord);

if you want to remove it in Sql Server like here

select substring(ColumnName, patindex('%[^0]%',ColumnName), 10)
Transformer
  • 6,963
  • 2
  • 26
  • 52
0

You also can use regex to get the value without leading zeros.

Code:

    Regex rx = new Regex(@"^0+(\d+)$");
    Console.WriteLine(rx.Replace("0000151010", @"$1") );

Result:

151010
Jack J Jun
  • 5,633
  • 1
  • 9
  • 27