Link to home
Start Free TrialLog in
Avatar of himabindu_nvn
himabindu_nvn

asked on

SQL Query

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
Avatar of ralmada
ralmada
Flag of Canada image

select country, max(zip)
from yourtable
group by country
Avatar of Bhavesh Shah
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

Avatar of himabindu_nvn
himabindu_nvn

ASKER

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.
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
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
how do you define a different format?

you either need a "format type" column or some simple test that we can perform...
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.
ASKER CERTIFIED SOLUTION
Avatar of lludden
lludden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
helped me to some extent