Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > Putting it Together > Databases

Reply
 
LinkBack Thread Tools
Old February 21st, 2008, 07:25 AM   #1
New Member
 

Join Date: Feb 2008
Location: UK
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 meth8200 is on a distinguished road
[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 February 21st, 2008, 07:36 AM   #2
Elite Veteran
SuperMember
 
welshstew's Avatar
 

Join Date: May 2007
Location: inside the outside
Posts: 3,909
Blog Entries: 14
Thanks: 9
Thanked 40 Times in 38 Posts
Rep Altering Power: 0 welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute
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
extreme sports clothing and t-shirts | twitter| web design
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 February 21st, 2008, 08:14 AM   #3
Rob
Elite Veteran
SuperMember
 
Rob's Avatar
 

Join Date: Jul 2003
Location: Southern UK
Age: 35
Posts: 3,126
Blog Entries: 7
Thanks: 28
Thanked 22 Times in 19 Posts
Rep Altering Power: 0 Rob is a jewel in the rough Rob is a jewel in the rough Rob is a jewel in the rough
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 - Webforumz Founder
Web Designer Support Network || Personal Project: Sanctuary for Student Midwives

Last edited by Rob; February 21st, 2008 at 08:16 AM..
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 February 21st, 2008, 08:22 AM   #4
New Member
 

Join Date: Feb 2008
Location: Poreč
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 puzz is on a distinguished road
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 February 21st, 2008, 08:31 AM   #5
Elite Veteran
SuperMember
 
welshstew's Avatar
 

Join Date: May 2007
Location: inside the outside
Posts: 3,909
Blog Entries: 14
Thanks: 9
Thanked 40 Times in 38 Posts
Rep Altering Power: 0 welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute
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
extreme sports clothing and t-shirts | twitter| web design
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 February 21st, 2008, 08:32 AM   #6
Elite Veteran
SuperMember
 
welshstew's Avatar
 

Join Date: May 2007
Location: inside the outside
Posts: 3,909
Blog Entries: 14
Thanks: 9
Thanked 40 Times in 38 Posts
Rep Altering Power: 0 welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute
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
extreme sports clothing and t-shirts | twitter| web design
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 February 21st, 2008, 08:38 AM   #7
New Member
 

Join Date: Feb 2008
Location: Poreč
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 puzz is on a distinguished road
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 February 21st, 2008, 08:54 AM   #8
New Member
 

Join Date: Feb 2008
Location: UK
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 meth8200 is on a distinguished road
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 February 25th, 2008, 06:17 AM   #9
New Member
 

Join Date: Feb 2008
Location: UK
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 meth8200 is on a distinguished road
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 February 25th, 2008, 08:21 AM   #10
New Member
 

Join Date: Feb 2008
Location: UK
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 meth8200 is on a distinguished road
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

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 Databases 13 January 2nd, 2008 10:28 PM
[SOLVED] joining 2 queries together to make 1 query AdRock Databases 0 November 18th, 2007 05:37 PM
[SOLVED] Nightmare query AdRock Databases 0 November 17th, 2007 11:50 AM
[SOLVED] Partial Search Query Aerdan Databases 5 November 7th, 2007 11:26 AM
[SOLVED] asp / sql query Anonymous User Classic ASP 4 November 19th, 2004 04:39 AM


Search Engine Optimization by vBSEO 3.2.0 RC8