using LINQ i am query a data for sorting and i am getting right answer.
here is my data & linq query……please have look.
void Main()
{
List<SearchResult> list = new List<SearchResult>() {
new SearchResult(){ID=1,Title="Geo Prism GEO 1995 GEO* - ABS #16213899"},
new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"},
new SearchResult(){ID=3,Title="Geo Prism GEO 1995 - ABS #16213899"},
new SearchResult(){ID=4,Title="JCB Excavator JCB- ECU P/N: 728/35700"},
new SearchResult(){ID=5,Title="Geo Prism GEO,GEO 1995 - ABS #16213899 GEO"},
new SearchResult(){ID=6,Title="dog"},
};
var to_search = new[] { "Geo", "JCB" }.Select(x => x.ToLower()).ToArray();
var result = from searchResult in list
let title = searchResult.Title.ToLower()
let key_string = to_search.FirstOrDefault(ts => title.Contains(ts))
orderby key_string == null ? -1 : title.Split(new[] { key_string }, StringSplitOptions.None).Length descending
group searchResult by key_string into Group
orderby Group.Count() descending
select Group;
result.Dump();
}
public class SearchResult
{
public int ID { get; set; }
public string Title { get; set; }
}

my question is what sql query i need to write as a result i will get same output in sql server 2000.
suppose my data is stored in sql server table like this way
Table : MyTable
------------------
ID Title
-----------
1 Geo Prism GEO 1995 GEO* - ABS #16213899
2 Excavator JCB - ECU P/N: 728/35700
3 Geo Prism GEO 1995 - ABS #16213899
4 JCB Excavator JCB- ECU P/N: 728/35700
5 Geo Prism GEO,GEO 1995 - ABS #16213899 GEO
6 Maruti gear box #ABS 4587
MY Edit
i fix some syntax issue in ur sql….just have look
CREATE FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
DECLARE @Sterm varchar(MAX)
SET @Sterm ='GEO JCB'
;WITH SearchResult (rnum, title)
as
(
(select 1 as rnum,'Geo Prism GEO 1995 GEO* - ABS #16213899' as title)
union all
(select 2 as rnum,'Excavator JCB - ECU P/N: 728/35700' as title)
union all
(select 3 as rnum,'Geo Prism GEO 1995 - ABS #16213899' as title)
union all
(select 4 as rnum,'JCB Excavator JCB- ECU P/N: 728/35700' as title)
union all
(select 5 as rnum,'Geo Prism GEO,GEO 1995 - ABS #16213899 GEO' as title)
union all
(select 6 as rnum,'dog' as title)
)
select rnum, title from SearchResult
join
( select lower(Items) as term
from dbo.Split(@Sterm , ' ')
) as search_terms
on lower(SearchResult.title) like '%' + search_terms.term +'%'
order by
search_terms.term,
(select count(*)
from dbo.Split(lower(SearchResult.title),' ')
where Items = search_terms.term
) desc
but it is not full filling my requirement.
1) query should return search term and as well as those rows also at end which has no relation with search term.
2) second sort should be there like those rows will come first where search term found maximum time like GEO & JCB.
u miss second order by descending based on occurrence of search term. if u see the image very properly then u can see what kind of out put i was asking here. thanks.
You can’t do it with SQL alone. You will need a stored routine to split the title string into substrings, then SQL can count the substrings that match your search.