SQL Group By Postcode Area

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 17
Reputation: jammiedude is an unknown quantity at this point 
Solved Threads: 1
jammiedude's Avatar
jammiedude jammiedude is offline Offline
Newbie Poster

SQL Group By Postcode Area

 
0
  #1
Aug 19th, 2009
Hi i am wondering if anyone has a solution for sorting by postcode area. I wanted to be able to pick off either the First letter or first two letters and group by this.

I have figured out how to group by the first to characters of the postcode using Left(x,2) but i wanted to be able to pick out the numeric values if there are any in this subsequent string from the left fuction.

i.e. if the postcode has "L1", i just want "L", or if postcode has "CH", i want "CH". Then to be grouped by these.

Anyone have any ideas?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,258
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 581
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: SQL Group By Postcode Area

 
0
  #2
Aug 19th, 2009
Aren't postal codes all numeric? Where do you live
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 17
Reputation: jammiedude is an unknown quantity at this point 
Solved Threads: 1
jammiedude's Avatar
jammiedude jammiedude is offline Offline
Newbie Poster

Re: SQL Group By Postcode Area

 
0
  #3
Aug 19th, 2009
Sorry, didnt take into account american readers.

UK post codes have letters and number in them and range from 5 characters to 8, it usually takes the form 1 or 2 letters, plus 2 or 3 numbers plus 2 more letters. ie "xx00 0xx" or "x0 00xx" or "xx00x 0xx"

I really just want to identify the first 1 or 2 letters before it finds any numeric values.

Many thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 17
Reputation: jammiedude is an unknown quantity at this point 
Solved Threads: 1
jammiedude's Avatar
jammiedude jammiedude is offline Offline
Newbie Poster

Re: SQL Group By Postcode Area

 
0
  #4
Aug 19th, 2009
Solved it!! But just for anyone else in the UK who is wanting the same solution without spending most of the day trying to trial and error their way through, use the following code:

  1. GROUP BY
  2. CASE WHEN ISNUMERIC(RIGHT(LEFT(C.Pcode,2),1)) = '0' THEN LEFT(C.pcode,2)
  3. ELSE LEFT(C.pcode,1)
  4. END

You must also use this CASE statement in the SELECT area, for all those who havent yet learnt aggregate's.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,258
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 581
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: SQL Group By Postcode Area

 
0
  #5
Aug 19th, 2009
Try something like
  1. (CASE WHEN code LIKE '[a-Z][a-Z]%' THEN SUBSTRING 2 ELSE SUBSTRING one END)

finish the substring part. I'm on my phone and non alpha chars are hard to use
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC