all groups > sql server new users > july 2006 >
You're in the

sql server new users

group:

Getting rid of leading 0s in strings


Getting rid of leading 0s in strings Orlando Acevedo
7/25/2006 12:07:46 PM
sql server new users: I am trying to use several tables that have one 10-character text field in
common.
Most of the records have a numeric expression, but some tables have leading
0's, and some don't.
I can't cast the field to numbers because there are some records that have
letters also.
What function can I use to get rid of all the 0s at the left of each record?

Thanks!

Re: Getting rid of leading 0s in strings Arnie Rowland
7/25/2006 12:13:28 PM
Hugo,

The replace() is excellent!

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


[quoted text, click to view]

Re: Getting rid of leading 0s in strings Roy Harvey
7/25/2006 12:44:50 PM
There is always Brute Force. 8-)

CREATE TABLE Whatever (x char(10))
INSERT Whatever values ('0123456789')
INSERT Whatever values ('0012345678')
INSERT Whatever values ('0001234567')
INSERT Whatever values ('0000123456')
INSERT Whatever values ('0000012345')
INSERT Whatever values ('0000001234')
INSERT Whatever values ('0000000123')

SELECT CASE WHEN SUBSTRING(x, 1,10) = REPLICATE('0',10)
THEN ''
WHEN SUBSTRING(x, 1, 9) = REPLICATE('0', 9)
THEN SUBSTRING(x,10, 1)
WHEN SUBSTRING(x, 1, 8) = REPLICATE('0', 8)
THEN SUBSTRING(x, 9, 2)
WHEN SUBSTRING(x, 1, 7) = REPLICATE('0', 7)
THEN SUBSTRING(x, 8, 3)
WHEN SUBSTRING(x, 1, 6) = REPLICATE('0', 6)
THEN SUBSTRING(x, 7, 4)
WHEN SUBSTRING(x, 1, 5) = REPLICATE('0', 5)
THEN SUBSTRING(x, 6, 5)
WHEN SUBSTRING(x, 1, 4) = REPLICATE('0', 4)
THEN SUBSTRING(x, 5, 6)
WHEN SUBSTRING(x, 1, 3) = REPLICATE('0', 3)
THEN SUBSTRING(x, 4, 7)
WHEN SUBSTRING(x, 1, 2) = REPLICATE('0', 2)
THEN SUBSTRING(x, 3, 8)
WHEN SUBSTRING(x, 1, 1) = REPLICATE('0', 1)
THEN SUBSTRING(x, 2, 9)
ELSE x
END as NoLeadingZeroes
FROM Whatever

NoLeadingZeroes
---------------
123456789
12345678
1234567
123456
12345
1234
123

Roy Harvey
Beacon Falls, CT

On Tue, 25 Jul 2006 12:07:46 -0400, "Orlando Acevedo"
[quoted text, click to view]
Re: Getting rid of leading 0s in strings Arnie Rowland
7/25/2006 1:31:35 PM
I would think that if you were concerned about the embedded spaces, you
could first replace() the embedded spaces with a placeholder, and then back,
e.g.,

replace( replace( ltrim( replace( replace( x, ' ', '~' ), '0', ' ' )), '
', '0' ),'~',' ' )


--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


[quoted text, click to view]

Re: Getting rid of leading 0s in strings Orlando Acevedo
7/25/2006 1:33:25 PM
BRUTE FORCE it will be.

Thank you very much Roy.


[quoted text, click to view]

Re: Getting rid of leading 0s in strings Roy Harvey
7/25/2006 2:47:38 PM
On Tue, 25 Jul 2006 20:28:22 +0200, Hugo Kornelis
[quoted text, click to view]

Very neat!

[quoted text, click to view]

Also nice, but I like the REPLACE better.

I figured there would be a few elegant solutions like these, but none
of them came to mind right away. It seemed to be a pretty good bet
nobody else where would write anything so brutal so it would at least
have the advantage of being different. 8-)

Re: Getting rid of leading 0s in strings Orlando Acevedo
7/25/2006 4:06:35 PM
Awesome!
That's a heck of a clever solution!

Thanks Hugo.

[quoted text, click to view]

Re: Getting rid of leading 0s in strings Hugo Kornelis
7/25/2006 8:28:22 PM
[quoted text, click to view]

Hi Orlando,

If brute force is not your thing, then there's also the possibility of
clever use of string functions.

The query below works, unless you have data with embedded spaces:

SELECT REPLACE(LTRIM(REPLACE(x, '0', ' ')), ' ', '0')
FROM Whatever;

And then there's the possibility of combining RIGHT and PATINDEX, to get
the query below. I only got it to fail on '0000000000'.

SELECT RIGHT(x, 11 - PATINDEX('%[^0]%', x))
FROM Whatever;

--
Re: Getting rid of leading 0s in strings Hugo Kornelis
7/25/2006 9:59:14 PM
[quoted text, click to view]

(snip)
[quoted text, click to view]

Hi Roy,

Agreed. I guess I should have credited the person I stole this from, but
I don't remember. I pick up so many good tricks in these groups that
it's hard to keep track of who posted what first. <g>

[quoted text, click to view]

It is somehow more elegant - but it's habit of messing up embedded
spaces is annoying. For correctness, you need the RIGHT / PATINDEX
version.

[quoted text, click to view]

Heh! So true!

--
Re: Getting rid of leading 0s in strings Hugo Kornelis
7/25/2006 11:27:48 PM
[quoted text, click to view]

Hi Arnie,

Good thinking!

--
AddThis Social Bookmark Button