Web Design and Development Forums

[SOLVED] Query postcode issue?

This is a discussion on "[SOLVED] Query postcode issue?" within the MySQL section. This forum, and the thread "[SOLVED] Query postcode issue? are both part of the Databases category.


Go Back   Webforumz.com > Databases > MySQL

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Feb 21st, 2008, 11:25   #1 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: UK
Posts: 10
[SOLVED] Query postcode issue?

Hi Everyone,

Basically i've got a postcode query problem, i have a column with postcodes like:

abc 123
abcd 123
ab 123

What im trying to do is query only the first set on digits without the 123.
So my output should be:

abc
abcd
ab

Ive used the following technique:

SELECT TRIM(SUBSTRING(dbTest.postCode,1,4)) AS shortPostCode

FROM dbTest

This is not accurate as it chucks out:

abc
abcd
ab 1 <-?

Because im using a substring 1,4 ab 1 happens.

Any solutions please?? any suggestions would be helpful!

Thanks

meth8200 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 21st, 2008, 11:36   #2 (permalink)
Section Manager -
Website Critique
 
welshstew's Avatar
 
Join Date: May 2007
Location: inside the outside
Posts: 1,093
Blog Entries: 10
Re: Query postcode issue?

trim the last three, rather than selecting the first few (which is what I think you are trying to do)

in excel this would be something like:
=TRIM(LEFT(A1,LEN(A1)-3))

not sure about sql as not done it in about 4 years
__________________
WelshStew
Section Manager

tierney rides tboard - uk site : xtreme wales - extreme clothing
welshstew is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 21st, 2008, 12:14   #3 (permalink)
Rob
Head Admin & CEO
 
Rob's Avatar
 
Join Date: Jul 2003
Location: bug busting new style
Age: 34
Posts: 3,310
Blog Entries: 7
Send a message via MSN to Rob Send a message via Skype™ to Rob
Re: Query postcode issue?

Nice solution

Ideally, the seperate post code parts would reside in different database fields, but as they do not you need to work around it differently.

Either have SQL handle trimming off the last elements, or do it once the result-set has been returned.

You can with PHP, or ASP or whatever trim everything from the post after and including the space.
__________________
Rob Collyer - SEO Specialist (8 years+)
Owner & Founder of Webforumz.com

My Mrs's site:- Student Midwife Forums

I am currently unavailable for private work
-------------------------------------------------

Last edited by Rob; Feb 21st, 2008 at 12:16.
Rob is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 21st, 2008, 12:22   #4 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: Poreč
Posts: 12
Re: Query postcode issue?

You can try something like this:

select substring(postcode,1,if(instr(postcode," ")=0,length(postcode),instr(postcode," "))) from my_table;

It will return part of your string from the first character to the first space...
puzz is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 21st, 2008, 12:31   #5 (permalink)
Section Manager -
Website Critique
 
welshstew's Avatar
 
Join Date: May 2007
Location: inside the outside
Posts: 1,093
Blog Entries: 10
Re: Query postcode issue?

in oracle sql it would be:

select Rtrim(substr(postcode, 0, LENGTH (postcode) -3 ) ) from address

it may be similar in mysql??
__________________
WelshStew
Section Manager

tierney rides tboard - uk site : xtreme wales - extreme clothing
welshstew is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 21st, 2008, 12:32   #6 (permalink)
Section Manager -
Website Critique
 
welshstew's Avatar
 
Join Date: May 2007
Location: inside the outside
Posts: 1,093
Blog Entries: 10
Re: Query postcode issue?

Quote:
Originally Posted by puzz View Post
You can try something like this:

select substring(postcode,1,if(instr(postcode," ")=0,length(postcode),instr(postcode," "))) from my_table;

It will return part of your string from the first character to the first space...
what if they don't put a space in?
__________________
WelshStew
Section Manager

tierney rides tboard - uk site : xtreme wales - extreme clothing
welshstew is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 21st, 2008, 12:38   #7 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: Poreč
Posts: 12
Re: Query postcode issue?

Quote:
Originally Posted by welshstew View Post
what if they don't put a space in?
If he is sure that the right part is always 3 characters long then: trim(substring(postcode,1,length(postcode)-3));
puzz is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 21st, 2008, 12:54   #8 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: UK
Posts: 10
Re: Query postcode issue?

thanks people, i've used puzz's solution although understanding -3 is also a benefit! cheers!
meth8200 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 25th, 2008, 10:17   #9 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: UK
Posts: 10
Re: Query postcode issue?

Hi People,

I know this is a mysql forum but would you know how this would work in sql enterprise

select substring(postcode,1,if(instr(postcode," ")=0,length(postcode),instr(postcode," "))) from my_table;

it doesnt like the if statement.......

works fine in mysql!

Cheers!!!
meth8200 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Feb 25th, 2008, 12:21   #10 (permalink)
Junior Member
 
Join Date: Feb 2008
Location: UK
Posts: 10
Re: Query postcode issue?

hi managed to sort it......no worries
meth8200 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Mysql Query not working nate2099 MySQL 13 Jan 3rd, 2008 02:28
[SOLVED] joining 2 queries together to make 1 query AdRock MySQL 0 Nov 18th, 2007 21:37
[SOLVED] Nightmare query AdRock MySQL 0 Nov 17th, 2007 15:50
[SOLVED] Partial Search Query Aerdan MySQL 5 Nov 7th, 2007 15:26
[SOLVED] asp / sql query Anonymous User ASP Forum 4 Nov 19th, 2004 08:39



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 17:31.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59