SQL Help

Soldato
Joined
13 Aug 2004
Posts
6,788
Location
Bedford
Say I have an address like

54 Solent Gardens

in a column in my database. How would i go about cutting it and move the Solent Gardens bit to a different column?

Halp
 
they are all [number] [place]

however, some numbers can only be one digit i.e

8 test drive

and soem can be

84 test land
 
Very quick and very dirty...

You'll need to create two fields (if you haven't already), one called HouseNumber, the other called StreetName
Code:
UPDATE Table SET 
   HouseNumber = LEFT(AddressField, CHARINDEX(' ', AddressField)),
   StreetName = RIGHT(AddressField, LEN(AddressField) - LEN(LEFT(AddressField, CHARINDEX(' ', AddressField))) - 1)

Obviously this won't work for the 'Flat 2a' scenario SimonC mentioned.
 
Hi

thanks for the help

is it possible to leave house number in the field it exists at the minute and simply cut the sstreet name and move that ?
 
Do it in 2 passes.
Create one new column (say streetname) and run an update to populate that column.
then I think runing the update again on the first column will work:

Code:
UPDATE Table SET 
   StreetName = RIGHT(AddressField, LEN(AddressField) - LEN(LEFT(AddressField, CHARINDEX(' ', AddressField))) - 1)

UPDATE Table SET 
   AddressField = LEFT(AddressField, CHARINDEX(' ', AddressField)),
CHARINDEX(' ', AddressField))) - 1)

I think that should work but test it first!
 
Back
Top Bottom