Solved

SQL Query

Posted on 2011-09-29
9
220 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
9 Comments
 
LVL 41

Expert Comment

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

Expert Comment

by:Bhavesh Shah
Comment Utility
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
Comment Utility
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
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 41

Expert Comment

by:ralmada
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
helped me to some extent
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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.
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now