Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Leading Zeros 1

Status
Not open for further replies.

gcoast

Programmer
Dec 17, 2003
39
US
What is the best way to replace leading zeros in a char field with blanks? Must keep the '2400' in positions 5-8.

'00002400' would become ' 2400'

Thanks,
gc
 
Are the records always four zeros and then four numbers you want to keep? Or does the number of numbers you want to keep vary?

And why do this anyway, why not just strip the leading zeros and right justifying the column in any reports or or application pages that show the number?

"NOTHING is more important in a database than integrity." ESquared
 
Take a look at this post I had made a while back regarding a similar situation. It describes a Function for padding values.

Convert to a number then pad it out with a space.

thread183-1480960

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
The field is always eight characters long but may contain numbers or characters. The number of leading zeros will vary.
The database is being supplied by a 3rd party and we cannot modify the way they are storing the data.

Thanks,

gc
 
There is a trick you could use.

First, replace 0's with space.
LTrim the data (Removing spaces from the beginning)
Replace Space with 0.

Ex:

Code:
Declare @Data VarChar(8)

Set @Data = '00002400'
Select @Data -- '00002400'

Set @Data = Replace(@Data, '0', ' ')
Select @Data -- '    24  '

Set @Data = LTrim(@Data)
Select @Data -- '24  '

Set @Data = Replace(@Data, ' ', '0')
Select @Data -- '2400'

Put it all together....
Code:
Set @Data = Replace(LTrim(Replace(@Data, '0', ' ')), ' ', '0')




-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks for the replies. But as I said I must keep the '2400' in positions 5-8. The data will at times contain numbers and letters so I can't convert to an int.
Just need the final result to be ' 2400'.

Thanks,

gc
 
Ok. then you can use a combination of the Pad Function and gmmastros' suggestion:

Code:
Set @Data = Padvalue(Replace(LTrim(Replace(@Data, '0', ' ')), ' ', '0'), 8, ' ')

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
Fine. Add another step.

Code:
Declare @Data VarChar(8)

Set @Data = '00002400'
Select @Data -- '00002400'

Set @Data = Replace(@Data, '0', ' ')
Select @Data -- '    24  '

Set @Data = LTrim(@Data)
Select @Data -- '24  '

Set @Data = Replace(@Data, ' ', '0')
Select @Data -- '2400'

Set @Data = Right('        ' + @Data, 8)
Select @Data -- '    2400'

putting it all together...

Code:
Set @Data = Right('        ' + Replace(LTrim(Replace(@Data, '0', ' ')), ' ', '0'), 8)

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Code:
SELECT RIGHT(SPACE(LEN(YourField))+
            CAST(CAST(YourField as int) As varchar(8)),
            LEN(YourField))
FROM YourTable
WHERE ISNUMERIC(YourField+'e0')=1

NOT TESTED WELL

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Thanks for all the replies,

Just what I needed.

gc
 
Another way (using obscure functions)

Code:
Declare @Data VarChar(8)

Set @Data = '00002400'

Select Stuff(@Data, 1, PatIndex('%[^0]%', @Data)-1, Space(PatIndex('%[^0]%', @Data)-1))


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
George, I think I will use your last post as it keeps me from specifying the length of the field in the select statement.

Thanks again,

gc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top