?
Solved

SQL Query

Posted on 2011-09-29
9
Medium Priority
?
232 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 1500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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