Home All Groups Group Topic Archive Search About
Author
7 Sep 2006 8:20 AM
Lasse Edsvik
Hello

I have a problem, I was given a table full of addresses and I found that
zipcode and city were in same column, so I need to run an update that
separates those.

Problem is that there are 2 spaces so I cant just split at a space.

Format: 123 45 City

How I do that?

/Lasse

Author
7 Sep 2006 8:27 AM
Lasse Edsvik
and i noticed that some are with only 1 space :(

as in Format: 12345 City


Show quote
"Lasse Edsvik" <lasse@nospam.com> wrote in message
news:%23$QdiZl0GHA.1256@TK2MSFTNGP02.phx.gbl...
> Hello
>
> I have a problem, I was given a table full of addresses and I found that
> zipcode and city were in same column, so I need to run an update that
> separates those.
>
> Problem is that there are 2 spaces so I cant just split at a space.
>
> Format: 123 45 City
>
> How I do that?
>
> /Lasse
>
>
Author
7 Sep 2006 8:57 AM
ML
Try this:

declare    @str1    varchar(256)
declare    @str2    varchar(256)

set    @str1 = '123 45 City'
set    @str2 = '12345 City'

select    replace(substring(@str1, 1, patindex('%[^0-9 ]%', @str1) - 1), ' ', '')
    ,replace(substring(@str2, 1, patindex('%[^0-9 ]%', @str2) - 1), ' ', '')


ML

---
http://milambda.blogspot.com/
Author
7 Sep 2006 8:59 AM
ML
Ok, both values this time:

declare    @str1    varchar(256)
declare    @str2    varchar(256)

set    @str1 = '123 45 City'
set    @str2 = '12345 City'

select    replace(substring(@str1, 1, patindex('%[^0-9 ]%', @str1) - 1), ' ', '')
    ,substring(@str1, patindex('%[^0-9 ]%', @str1), datalength(@str1))

select    replace(substring(@str2, 1, patindex('%[^0-9 ]%', @str2) - 1), ' ', '')
    ,substring(@str2, patindex('%[^0-9 ]%', @str2), datalength(@str2))



ML

---
http://milambda.blogspot.com/
Author
7 Sep 2006 9:45 AM
Lasse Edsvik
ML,

thx how would it work if it was a [anychars + space + anychars] ? :)

i.e abc a4sadf, 23d3e 4342d




Show quote
"ML" <M*@discussions.microsoft.com> wrote in message
news:5B3A9815-18ED-45D0-8648-221EC5335FFD@microsoft.com...
> Ok, both values this time:
>
> declare @str1 varchar(256)
> declare @str2 varchar(256)
>
> set @str1 = '123 45 City'
> set @str2 = '12345 City'
>
> select replace(substring(@str1, 1, patindex('%[^0-9 ]%', @str1) - 1), ' ',
'')
> ,substring(@str1, patindex('%[^0-9 ]%', @str1), datalength(@str1))
>
> select replace(substring(@str2, 1, patindex('%[^0-9 ]%', @str2) - 1), ' ',
'')
> ,substring(@str2, patindex('%[^0-9 ]%', @str2), datalength(@str2))
>
>
>
> ML
>
> ---
> http://milambda.blogspot.com/
Author
7 Sep 2006 9:57 AM
ML
That would require a slightly different approach - please post more sample
data.

However, to tackle this one the proper SQL-way (using a set-based approach)
you'd need a list of all valid post codes. Can you get a valid list?

A set-based solution usually outperforms any solution based on procedural
logic. It's also less difficult to understand and maintain.


ML

---
http://milambda.blogspot.com/
Author
7 Sep 2006 11:39 PM
Steve Dassin
"ML" <M*@discussions.microsoft.com> wrote in message
news:3191A8C2-6CCE-43C5-ADA6-E590513C9F28@microsoft.com...
> .
> It's also less difficult to understand and maintain.

For the regulars on this ng the matter is SET-tled.For the rest of the
industry it's precisely the opposite. Forty years of history and MSs own
future direction substantiate such a conclusion.For many sql is a
crutch for the lost ability to see a solution any other way.

http://racster.blogspot.com
Author
8 Sep 2006 4:57 AM
Arnie Rowland
Excellent Steve. If you are going to keep entertaining us, then boldly plug
your product!

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

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


Show quote
"Steve Dassin" <rac4sqlnospam@net> wrote in message
news:uon9I9u0GHA.1588@TK2MSFTNGP02.phx.gbl...
> "ML" <M*@discussions.microsoft.com> wrote in message
> news:3191A8C2-6CCE-43C5-ADA6-E590513C9F28@microsoft.com...
>> .
>> It's also less difficult to understand and maintain.
>
> For the regulars on this ng the matter is SET-tled.For the rest of the
> industry it's precisely the opposite. Forty years of history and MSs own
> future direction substantiate such a conclusion.For many sql is a
> crutch for the lost ability to see a solution any other way.
>
> http://racster.blogspot.com
>
>
>
Author
8 Sep 2006 5:34 AM
Steve Dassin
"Arnie Rowland" <ar***@1568.com> wrote in message
news:%23kWZENw0GHA.1268@TK2MSFTNGP02.phx.gbl...
> Excellent Steve. If you are going to keep entertaining us, then boldly
plug
> your product!

Au Contraire.. could it be you have error'd in your interpretation?
All dots do not lead back to Rac.After all I didn't give the rac link.
Of course 'entertainment' is highly subjective.So I will accept a
50% solution:( :)

'http://racster.blogspot.com'
Author
8 Sep 2006 7:10 AM
ML
Yes, great products sell themselves.


ML

---
http://milambda.blogspot.com/
Author
8 Sep 2006 5:39 AM
Steve Dassin
"ML" <M*@discussions.microsoft.com> wrote in message
news:71D0E632-33A4-45E0-AF34-3DF0C7829A7F@microsoft.com...
> Yes, great products sell themselves.

Bing, Bam,Woush,Ouch,Ouch,Ouch.
1,2,3.....Your Out!

Good Zinger! even if based on the wrong path :-)

You seem to be a good sport.We can get along:)

best,
steve
Author
8 Sep 2006 3:41 PM
SQL Menace
Here is another way

declare @str1   varchar(256)
declare @str2   varchar(256)
declare @str3   varchar(256)


set     @str1 = '123 45 City'
set     @str2 = '12345 City'
set     @str3 = '12345        City'


select  ltrim(right(@str1,patindex('% %', reverse(@str1)))) as City,
        left(@str1,(len(@str1) -(patindex('% %', @str1))))


select  ltrim(right(@str2,patindex('% %', reverse(@str2)))) as City,
        left(@str2,(len(@str2) -(patindex('% %', @str2))))

select  ltrim(right(@str3,patindex('% %', reverse(@str3)))) as City,
        left(@str3,(len(@str3) -(patindex('% %', @str3))))

Denis the SQL Menace
http://sqlservercode.blogspot.com/

ML wrote:
Show quote
> Ok, both values this time:
>
> declare    @str1    varchar(256)
> declare    @str2    varchar(256)
>
> set    @str1 = '123 45 City'
> set    @str2 = '12345 City'
>
> select    replace(substring(@str1, 1, patindex('%[^0-9 ]%', @str1) - 1), ' ', '')
>     ,substring(@str1, patindex('%[^0-9 ]%', @str1), datalength(@str1))
>
> select    replace(substring(@str2, 1, patindex('%[^0-9 ]%', @str2) - 1), ' ', '')
>     ,substring(@str2, patindex('%[^0-9 ]%', @str2), datalength(@str2))

>
>
> ML
>
> ---
> http://milambda.blogspot.com/
Author
8 Sep 2006 2:54 PM
--CELKO--
You can nest REPLACE() functions about 32 levels deep

UPDATE Addresses
--remove all digits
SET city =
REPLACE (
REPLACE (
  ..
  REPLACE (city_zip, '9', ''),
  '8', ''),
..
'0', '')
--remove all alphas and spaces
zip =
REPLACE (
REPLACE (
  ..
  REPLACE (LOWER(city_zip), 'z', ''),
  'y', ''),
..
'a', '')
' ', '');

Then make another update to clean up leading and double spacing in the
city column.
Author
8 Sep 2006 3:47 PM
Anith Sen
>> You can nest REPLACE() functions about 32 levels deep

I doubt if REPLACE() has a 32 level nesting restriction.

With SQL Server, REPLACE nesting levels can go way upto more than 500
levels, depending on the number of characters in its first argument.

--
Anith
Author
8 Sep 2006 3:59 PM
SQL Menace
Here are 44 of them

declare @v varchar(500)
select @v ='qwertyuiopasdfghjklzxcvbnm1234567890)(*&^%$#@!'

select replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(
replace(replace(@v,'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8','')
,'9',''),'0',''),'A',''),'B',''),'C',''),'D','')
,'E',''),'F',''),'G',''),'H',''),'I',''),'J','')
,'K',''),'L',''),'M',''),'N',''),'O',''),'P','')
,'Q',''),'R',''),'S',''),'T',''),'U',''),'V','')
,'W',''),'X',''),'Y',''),'Z',''),'(',''),')','')
,'*',''),'&',''),'^',''),'%',''),'$',''),'#','')


Denis the SQL Menace
http://sqlservercode.blogspot.com/


Anith Sen wrote:
Show quote
> >> You can nest REPLACE() functions about 32 levels deep
>
> I doubt if REPLACE() has a 32 level nesting restriction.
>
> With SQL Server, REPLACE nesting levels can go way upto more than 500
> levels, depending on the number of characters in its first argument.
>
> --
> Anith

AddThis Social Bookmark Button