lundi 30 mars 2015

Search column for a string in every space separated value

Suppose I have a column named CustomerNames in database with values:



  1. Vins et alcools Chevalier

  2. Magazzini Alimentari Riuniti

  3. Hungry Owl All-Night Grocers

  4. Split Rail Beer & Ale

  5. Alfreds Futterkiste


I want to make a query which will search only for the first letters of FIRSTNAME and LASTNAME, and list the names in ASC order based on FIRSTNAME then LASTNAME. for example if I search for "al" in above table, it should return me something like below:



  1. Alfreds Futterkiste

  2. Vins et alcools Chevalier

  3. Split Rail Beer & Ale

  4. Magazzini Alimentari Riuniti

  5. Hungry Owl All-Night Grocers


I have used below query.



SELECT * FROM Customers where CustomerName LIKE 'al%' OR CustomerName LIKE '% al%' ORDER BY CustomerName


Output:



  1. Alfreds Futterkiste

  2. Hungry Owl All-Night Grocers

  3. Magazzini Alimentari Riuniti

  4. Split Rail Beer & Ale

  5. Vins et alcools Chevalier


Which is orderingt results only on the basis of firstnames. But I want a query which gives output using firstnames and then lastnames. Thanks.


Aucun commentaire:

Enregistrer un commentaire