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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Sub String

Author  Topic 

NeilC
Yak Posting Veteran

55 Posts

Posted - 2007-08-08 : 12:26:17
I have a column with the following data:

Owner
ASHDOWN, AR 71822

I need to grab the zip code and insert it into a zip column - how can I do this with sub string? Or is there an easier way?

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-08-08 : 12:30:43
[code]
DECLARE @zip varchar(50)
select @zip = 'ASHDOWN, AR 71822'

select RIGHT(@zip, CHARINDEX(' ', REVERSE(@zip)))
[/code]

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

NeilC
Yak Posting Veteran

55 Posts

Posted - 2007-08-08 : 12:50:38
Thanks Spirit1, I have over 10,000 records that I need to work on and in this example @zip is hardcoded. I need to grab the zip out of the owner column in every record and insert it into a new column called zip - how can that be done dynamically?
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-08-08 : 13:02:53
select RIGHT(YourZipColumn, CHARINDEX(' ', REVERSE(YourZipColumn)))
from yourTable

this will only work if your zips are at the end of each row,a space is before them asnd they have no space in them.


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page
   

- Advertisement -