If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Truncating part of a zip code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-03, 19:00
feign3 feign3 is offline
Registered User
 
Join Date: Jan 2003
Posts: 19
Wink Truncating part of a zip code

Sorry for the remedial question (after searching here and google and my pl/sql books for 30 minutes)....

How do you truncate a record returned from a query?

I am pulling a zip code from the database and want to get rid of the hyphen and last 4 digits. I.E. 60691-4467 ---> 60691

I don't know why I can't find anything on something this rediculous. If anyone has any helpful links they can give me for good reference for this kind of silly stuff I'd appreciate it as well.

Thanks!
Reply With Quote
  #2 (permalink)  
Old 02-05-03, 05:46
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: Truncating part of a zip code

The exact answer depends on your DBMS, but will be similar to this Oracle solution:

1) Use the INSTR function to find the hyphen:
INSTR( zip_code, '-' )
This returns 6 in your example, and 0 if there is no hyphen

2) Use the SUBSTR function to get just the part before the hyphen:
SUBSTR(zip_code,1,5)

To neatly handle the case where the hyphen is absent, you could do this:

SELECT SUBSTR( zip_code, 1, INSTR( zip_code||'-', '-') - 1 )
FROM ...

I think INSTR may be called CONTAINS, and SUBSTR called SUBSTRING in SQL Server.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 02-05-03, 08:03
feign3 feign3 is offline
Registered User
 
Join Date: Jan 2003
Posts: 19
Re: Truncating part of a zip code

Sorry Tony.... I am using 8i. And thank you for the suggestions!!
Reply With Quote
  #4 (permalink)  
Old 02-05-03, 08:49
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Can you use the LEFT function??

LEFT ( character_expression , integer_expression )

left(zipcode,6)

would give you what you want.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On