Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Remove leading zeros from street codes

Author  Topic 

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-06-21 : 05:11:46
Hi all

I am trying to write a SQL code that updates all street codes within my database and remove leading zeros where appropriate.

The table that holds this is called central_street where Street_code is set to data type Character.

Anyone know how I could do this? I am quite new to SQL so am not sure whether I should use Update statement or a standard SELECT.

Kind regards

J

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-06-21 : 05:14:37
Are all street codes numeric? In that case:

UPDATE central_street SET Street_code = CAST(Street_code AS int)

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-21 : 08:08:06
or


UPDATE central_street SET Street_code = Street_code*1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-21 : 08:11:07
quote:
Originally posted by madhivanan

or


UPDATE central_street SET Street_code = Street_code*1

Madhivanan

Failing to plan is Planning to fail


or Street_code + 0


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-21 : 08:28:42
If not all values are numeric then try this:

update central_street
set street_code = stuff(street_code,1,patindex('%[^0]%',street_code)-1,'')
where left(street_code,1)='0'



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-21 : 08:33:07
quote:
Originally posted by webfred

quote:
Originally posted by madhivanan

or


UPDATE central_street SET Street_code = Street_code*1

Madhivanan

Failing to plan is Planning to fail


or Street_code + 0


No, you're never too old to Yak'n'Roll if you're too young to die.


Yes. See this
http://beyondrelational.com/blogs/madhivanan/archive/2008/07/21/ordering-interger-values-stored-in-varchar-column.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-21 : 09:35:06
quote:
Originally posted by Lumbago

Are all street codes numeric? In that case:

UPDATE central_street SET Street_code = CAST(Street_code AS int)

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com




If the above query is modified this way it will take care of the street codes that are not numeric.

UPDATE central_street SET Street_code = CAST(Street_code AS int) where IsNumeric(Street_code)=1


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-21 : 09:38:09
IsNumeric() isn't reliable for this.

See for example: select convert(int,'1.0')


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-21 : 09:39:49
Also refer this for more examples
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/enhanced-isnumeric-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-21 : 09:49:30
quote:
Originally posted by webfred

IsNumeric() isn't reliable for this.

See for example: select convert(int,'1.0')


No, you're never too old to Yak'n'Roll if you're too young to die.



Well what if I do something like this.

update central_street SET Street_code =convert(int,convert(decimal(18,2),Street_code)) where ISNUMERIC(name)=1


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page
   

- Advertisement -