0

I have table called Property and the data is like

   pid     city           state          state_abb      address1   address2
    x1     NewCity        NHANy              NH          xxxx      gfg
    x2     Gloucester     Manchestar         MA          newAde    xxxx
    x3     OtherC         NewYork            NY          yyyy

I want a query with search keyword to display order like (state or state_abb), city, address1, adress2

For ex:

If I search keyword with New The result output should

    pid   city         state        state_abb   address1    address2
     x3   OtherC       NewYork         NY       yyyy 
     x1   NewCity      NHANy           NH       xxxx        gfg
     x2   Gloucester   Manchester      MA       newAde      xxxx

I don't want unmatched rows.I want display only matched rows.

Thanks in advance

3
  • 4
    What did you try? Please post the queries you attempted and explain where you are having problems. Commented Sep 28, 2011 at 20:13
  • @Oded Agreed, I don't want to type the whole query, but am willing to help... Commented Sep 28, 2011 at 20:15
  • Thank you for your reply i am poor in sql so plz Give me some idea i can try Commented Sep 28, 2011 at 20:18

4 Answers 4

3

It think the question is how to order the results, not how to perform the search. Try this as your ORDER BY clause

order by
 case when charindex('NEW',state) >0 then 1000 else 0 end desc,
 case when charindex('NEW',city) >0 then 100 else 0 end desc, 
 case when charindex('NEW',address) >0 then 10 else 0 end desc

Where clause to only get matching rows

 select * from Property where
        city     like '%New%' or
        state    like '%New%' or
        address1 like '%New%' or
        address2 like '%New%'
Sign up to request clarification or add additional context in comments.

2 Comments

Can u tell me the query i dont want unmatched data rows
If you don't want unmatched rows, you need a where clause like Jim Garrisson suggested below. I'll add it to my answer above.
0

You could try something like this:

SELECT pid, city, state, stat_abb, address1, address2
from property
where city like '%New%'
OR state like '%New%'
OR address1 like '%New%'

2 Comments

of course, searching on several fields with OR and also using LIKE with a leading % will completely and utterly avoid any possible indices to be used, and thus performance will be horribly slow....
@marc_s agreed but with the info he provided it gives him a startng point
0

This should get you started:

select * from Property where
    city     like '%New%' or
    state    like '%New%' or
    address1 like '%New%' or
    address2 like '%New%'

I assume you don't want to look at the pid column.

5 Comments

of course, searching on several fields with OR and also using LIKE with a leading % will completely and utterly avoid any possible indices to be used, and thus performance will be horribly slow....
@marc_s agreed, but the OP hasn't given much context. This could be perfectly usable if the table size isn't large
indeed - just wanted to point out that this approach is not be the best to scale to large numbers of rows .... for small numbers, it's irrelevant
"Its not working" is not enough information. What do you mean?
Its giving same table output not getting order
0
select *
from Property 
order by case
           when state     like 'New%' then 1
           when state_abb like 'New%' then 1
           when city      like 'New%' then 2
           when address1  like 'New%' then 3
           when address2  like 'New%' then 4
         end   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.