sql search query with like operator

fc123

I have a table named infoone with two columns address and locationCITY.

I want to write a search query.

If user searches for Edmonton, it should return all records that have city Edmonton.

If user searches for 12 main street Edmonton, then it should return all respective records. 12 main street is the address and Edmonton is the city

What query I have is following,

string sql = "select PLACEID,LEFT(CONVERT(NVARCHAR(1000),description),500)+'...' as des1,LOCATIONCITY,ADDRESS,DateT,RENT from infoone where  ";
    sql += "(((address like '%"+txtSearch.Text+"%')  or (address like '')) and ((locationcity like '%"+txtSearch.Text+"%') or  (locationcity like '')) and ((address LIKE '%"+txtSearch.Text+"%')  or (address like '')))";

Above query is not returning anything when I search:

main street Edmonton

User will also be allowed to search without city like: 12th main street

What I am doing wrong?

Please help

Thanks

Sparky

Free form searching for addresses is very difficult.

Let's look at your examples

Address         locationCity
12 Main Street  Edmonton
456 Thomas Ave  St Martin

Possible searchs

  • Edmonton - If only 1 word, should we assume it is a city? If so, how would the user find St. Martin?
  • 12 Main Street Edmonton - Now, how do you know Edmonton is the city? Could the user search for just a street name?

I would suggest that your interface accepts two columns, one for address and one for city, it will make searching much easier.

where  <other conditions>
AND
(locationcity like '%CitySearchFld%' and address like '%AddresssSearchFld%')

No need to search for empty, because if the user leaves the field blank, a search of %% will match all rows

Other considerations

What happens in the user searches for

12 Main St

or

Edmenton

Abbreviations? Misspellings?

To handle abbreviations, I would build a stop-word list, which would remove common abbreviations from the address field, things like St, Street, Avenue, Ave, etc. So the search becomes

12 Main

I'd hate to miss a record because I wasn't sure if it was st or Street in the table.

You can also use a function know as Soundex (native SQL) or Metaphone (custom SQL or CLR) to deal with misspellings...

Good luck

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Rails SQL QUERY Like

分類Dev

SQL Query for a search function

分類Dev

LIKE operator in SQL Server not working as expected

分類Dev

SQL LIKE operator with aaa% having strange behavior

分類Dev

SQL server : Replace the value using like operator

分類Dev

SQL query search within a string

分類Dev

mysql Nodejs connection.query like operator error

分類Dev

Sql Like query for Firebase Database Reference with MaterialSearchBar

分類Dev

SQL Like query with reverse name return result

分類Dev

REGEXP_LIKE、oracle、sql、query、understanding

分類Dev

Hibernate search - cconvert sql to lucene query

分類Dev

SQL Query to do a reverse CONTAINS search?

分類Dev

SQL search query with dynamically build where condition

分類Dev

Creating a search query with wildcard in SQL2

分類Dev

Coldfusion Solr Search-SQLのLIKE%string%に相当

分類Dev

Using like to query 100% key word in SQL Server

分類Dev

Oracle SQL query taking too long like 60 minutes to execute

分類Dev

How should I write SQL query that contains LIKE in PHP?

分類Dev

What is Retrofit OR operator for search

分類Dev

How to perform a LIKE query using multiple keywords from search field using mysqli prepared statement

分類Dev

Subselect on multiples values with like operator

分類Dev

Joining Using LIKE operator in Neteeza

分類Dev

how to use "like" operator in android?

分類Dev

SQLite query > operator not working

分類Dev

SQL word wide "%like%" search in Entity Framework 6 using lambda expression

分類Dev

Unary NOT operator in Oracle text search

分類Dev

Cannot use 'in' operator to search for '728' in

分類Dev

Linq search text using 'and' operator

分類Dev

Access SQL syntax error (missing operator) when query contains multiple JOINs

Related 関連記事

  1. 1

    Rails SQL QUERY Like

  2. 2

    SQL Query for a search function

  3. 3

    LIKE operator in SQL Server not working as expected

  4. 4

    SQL LIKE operator with aaa% having strange behavior

  5. 5

    SQL server : Replace the value using like operator

  6. 6

    SQL query search within a string

  7. 7

    mysql Nodejs connection.query like operator error

  8. 8

    Sql Like query for Firebase Database Reference with MaterialSearchBar

  9. 9

    SQL Like query with reverse name return result

  10. 10

    REGEXP_LIKE、oracle、sql、query、understanding

  11. 11

    Hibernate search - cconvert sql to lucene query

  12. 12

    SQL Query to do a reverse CONTAINS search?

  13. 13

    SQL search query with dynamically build where condition

  14. 14

    Creating a search query with wildcard in SQL2

  15. 15

    Coldfusion Solr Search-SQLのLIKE%string%に相当

  16. 16

    Using like to query 100% key word in SQL Server

  17. 17

    Oracle SQL query taking too long like 60 minutes to execute

  18. 18

    How should I write SQL query that contains LIKE in PHP?

  19. 19

    What is Retrofit OR operator for search

  20. 20

    How to perform a LIKE query using multiple keywords from search field using mysqli prepared statement

  21. 21

    Subselect on multiples values with like operator

  22. 22

    Joining Using LIKE operator in Neteeza

  23. 23

    how to use "like" operator in android?

  24. 24

    SQLite query > operator not working

  25. 25

    SQL word wide "%like%" search in Entity Framework 6 using lambda expression

  26. 26

    Unary NOT operator in Oracle text search

  27. 27

    Cannot use 'in' operator to search for '728' in

  28. 28

    Linq search text using 'and' operator

  29. 29

    Access SQL syntax error (missing operator) when query contains multiple JOINs

ホットタグ

アーカイブ