-
June 25th, 2007, 01:25 AM
#1
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.
-
June 25th, 2007, 02:15 AM
#2
Re: How to remove leading Zeros
Hi Rad,
Try something like this:
Code:
string nvarchar = "0001234";
nvarchar = nvarchar.Replace("0", "");
-
June 25th, 2007, 02:16 AM
#3
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);
-
June 25th, 2007, 02:23 AM
#4
Re: How to remove leading Zeros
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
-
June 25th, 2007, 02:25 AM
#5
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
-
June 25th, 2007, 02:28 AM
#6
Re: How to remove leading Zeros
Try this:
Code:
char[] charsToTrim = { '0'};
string nvarchar = "000012340";
string newString = nvarchar.TrimStart(charsToTrim);
Laitinen
-
June 25th, 2007, 04:09 AM
#7
Re: How to remove leading Zeros
Hi Laitinen,
Thanks a lot for your response...
Best regards,
Radhakrishnan
-
June 26th, 2007, 04:25 AM
#8
Re: How to remove leading Zeros
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
-
September 13th, 2010, 10:37 PM
#9
Re: How to remove leading Zeros
string paddedString = "000012340";
string newString = paddedString.TrimStart("0".ToCharArray());
-
September 15th, 2010, 12:46 AM
#10
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.......
-
September 16th, 2010, 01:25 PM
#11
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?
-
August 9th, 2011, 09:56 PM
#12
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
-
August 10th, 2011, 12:58 AM
#13
Re: How to remove leading Zeros
Originally Posted by mavy-online
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.
-
August 10th, 2011, 12:59 AM
#14
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.
-
August 10th, 2011, 01:27 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|