| Welcome to Webforumz.com. |
|
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

|
|
|
Feb 21st, 2008, 11:36
|
#2 (permalink)
|
|
Section Manager - Website Critique
Join Date: May 2007
Location: inside the outside
Posts: 1,093
|
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
|
|
|
Feb 21st, 2008, 12:14
|
#3 (permalink)
|
|
Head Admin & CEO
Join Date: Jul 2003
Location: bug busting new style
Age: 34
Posts: 3,310
|
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.
|
|
|
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...
|
|
|
Feb 21st, 2008, 12:31
|
#5 (permalink)
|
|
Section Manager - Website Critique
Join Date: May 2007
Location: inside the outside
Posts: 1,093
|
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??
|
|
|
Feb 21st, 2008, 12:32
|
#6 (permalink)
|
|
Section Manager - Website Critique
Join Date: May 2007
Location: inside the outside
Posts: 1,093
|
Re: Query postcode issue?
Quote:
Originally Posted by puzz
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?
|
|
|
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
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));
|
|
|
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!
|
|
|
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!!! 
|
|
|
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
|
|
|
| Thread Tools |
|
|
| 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
HTML code is Off
|
|
|
|
|
|