Solved

SQL Query

Posted on 2011-09-29
9
229 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

735 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