Solved

SQL Query

Posted on 2011-09-29
9
230 Views
Last Modified: 2012-05-12
I have a table with two fields Country and Zip. say like
country-zip
US      60074
US      99001-9900
us      99000
IN      600741
IN      834938
CA      1A2 C3G
CA      9K0 I9Y
PK      999
GB      9999999


I want the result to be something like this :
country-zip
US      99001-9900
us      99000
IN      600741
CA      1A2 C3G
PK      999
GB      9999999

It can choose any zip for one country if there are two.
Please help
0
Comment
Question by:himabindu_nvn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 36815803
select country, max(zip)
from yourtable
group by country
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 36815839
hi,

it seems your search is case-sensitive.

i found this.

http://blog.sqlauthority.com/2007/04/30/case-sensitive-sql-query-search/

it might helps
select country, max(zip) zip
from yourtable COLLATE Latin1_General_CS_AS = 'casesearch'
group by country

Open in new window

0
 

Author Comment

by:himabindu_nvn
ID: 36815962
I need to retrive the data based on the zipcode format like if for US there the 2 different formats like 60074 and 60074-9430 In this case i need to retrive both the records.
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 41

Expert Comment

by:ralmada
ID: 36816061
select country, format, max(zip) as zip
from (
      select country, case when len(zip) > len(replace(zip, '-', '')) then 1 else 0 end as format, zip
      from yourtable
) a
group by country, format
0
 
LVL 41

Expert Comment

by:ralmada
ID: 36816078
or like this

select country, format, max(zip) as zip
from (
      select country, case when charindex('-', zip) > 0 then 1 else 0 end as format, zip
      from yourtable
) a
group by country, format
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36816094
how do you define a different format?

you either need a "format type" column or some simple test that we can perform...
0
 
LVL 1

Expert Comment

by:dannocracker
ID: 36816104
You'll want to use regular expression type search. See this site for more info. http://msdn.microsoft.com/en-us/magazine/cc163473.aspx.  I find it easier to develop regex code in c# and deploy as CLR.
0
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
ID: 36817663
select country, max(zip)
from yourtable
WHERE LEN(zip) > 5
group by country
UNION
select country, max(zip)
from yourtable
WHERE LEN(zip) <= 5
group by country
0
 

Author Closing Comment

by:himabindu_nvn
ID: 36988085
helped me to some extent
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question