Results 1 to 5 of 5

Thread: How can I Remove the Leading Zeros in SQL?

  1. #1
    Join Date
    Jul 2006
    Posts
    128

    How can I Remove the Leading Zeros in SQL?

    Hello friends,
    I have recently started with the SQL. I have done little use of SQL as the backend of my assignment. But I am getting frustrated because I am unable to remove the starting zeroes. I have tried lot of different statements but that was not so useful. Can anyone out there tell me how can I Remove the Leading Zeros in SQL..?? Please help me soon as possible..!!
    "Every man is guilty of all the good he did not do". - Voltaire

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How can I Remove the Leading Zeros in SQL?

    I have provided you with the code that is used for removing the leading zeroes from a particular field. Lets say for an example, if the field contains '0005BC', the select statement needs to return the data as '5BC'. I think that you were asking the same thing. ust look the following code which does the same :
    Code:
    select substring(ColumnName, patindex('%[^0]%',ColumnName), 10)
    Hope that it will help you.!!

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How can I Remove the Leading Zeros in SQL?

    I would like to ask you that will your data always need to be stripped of the leading zeros? If you want to stripped of the leading zeros always, then it would be better to fix the data in the table and when it is entered or imported than to run a function in every select. If you are using for some cases and some situations, then using the code would be good. And if you want to remove the leading zeroes every time, then I would like to suggest you to make the changes in the database, instead of using the code everytime.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How can I Remove the Leading Zeros in SQL?

    If you are having the column called productno which consists of values which are having the leading zeroes like "000123, 000124, 000125,...etc". Then you can remove the leading zeroes from the column by using the code that i have mentioned below :
    Code:
    select productno, cast(productno as bigint), cast(productno as int)
    from <YourTableNameHere>
    by doing this the leading zeroes will get removed.

  5. #5
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How can I Remove the Leading Zeros in SQL?

    I am providing you with the code which is used for updating statement to remove leading zeros from a column. I have assumed that the column is a varchar field. This code will work even the number of the zeroes varies. Means if one variable is having 2 zeroes and one variable is having 3 zeroes then also this code will work properly.
    Code:
    UPDATE table
    SET column=LTRIM(column,'0');
    Hope that you find this code useful.

Similar Threads

  1. How to Format a cell to keep leading zeros in Microsoft Excel
    By Raju Chacha in forum MS Office Support
    Replies: 3
    Last Post: 14-01-2012, 07:05 PM
  2. Need help, leading zeros in java
    By loukassm in forum Software Development
    Replies: 1
    Last Post: 15-12-2010, 12:52 PM
  3. How to remove leading whitespace from each line?
    By Conraad in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 10:14 PM
  4. java program to add leading zeros to a number.
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 05-02-2010, 07:15 PM
  5. EXCEL not display leading zeros
    By Aasha in forum Windows Software
    Replies: 3
    Last Post: 22-10-2009, 06:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,194,598.89056 seconds with 16 queries