| Author | Topic | 
                            
                                    | sidharth.banyalStarting Member
 
 
                                        11 Posts | 
                                            
                                            |  Posted - 2010-01-07 : 07:29:05 
 |  
                                            | How can i sort result from select statement accordin to search criteria?Select * from dbemp where empname like '%'+@searchtext+'%' Now i need that results which are having @searchtext at begining should be placed above those which are having @searchtext in middle and in same way those having @searchtext in middle should be above those which are having @searchtext in last.Any help will be highly appreciated.Thanks in advance.Sidharth Banyal |  | 
       
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 07:47:34 
 |  
                                          | Select * from dbemp where empname like '%'+@searchtext+'%' order by charindex('%'+@searchtext+'%' ,empname )MadhivananFailing to plan is Planning to fail |  
                                          |  |  | 
                            
                       
                          
                            
                                    | balaganapathy.nStarting Member
 
 
                                    18 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 07:49:23 
 |  
                                          | I think this way.Select * from dbemp where empname like '%'+@searchtext+'%'  ORDER BY CHARINDEX( @searchtext, empname, 0) ASCbalaganapathy n.Anything you can imagine is real. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 07:52:39 
 |  
                                          | quote:You dont need 0 in the charindexSee my previous replyMadhivananFailing to plan is Planning to failOriginally posted by balaganapathy.n
 I think this way.Select * from dbemp where empname like '%'+@searchtext+'%'  ORDER BY CHARINDEX( @searchtext, empname, 0) ASCbalaganapathy n.Anything you can imagine is real.
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | balaganapathy.nStarting Member
 
 
                                    18 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 08:02:27 
 |  
                                          | quote:Yes, it is optional.but, I think it should be only @searchtext instead of '%'+@searchtext+'%' in the CHARINDEX function.balaganapathy n.Anything you can imagine is real.Originally posted by madhivanan
 
 quote:You dont need 0 in the charindexSee my previous replyMadhivananFailing to plan is Planning to failOriginally posted by balaganapathy.n
 I think this way.Select * from dbemp where empname like '%'+@searchtext+'%'  ORDER BY CHARINDEX( @searchtext, empname, 0) ASCbalaganapathy n.Anything you can imagine is real.
 
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 08:09:10 
 |  
                                          | quote:Yes. Only in patindex you need itOriginally posted by balaganapathy.n
 
 quote:Yes, it is optional.but, I think it should be only @searchtext instead of '%'+@searchtext+'%' in the CHARINDEX function.balaganapathy n.Anything you can imagine is real.Originally posted by madhivanan
 
 quote:You dont need 0 in the charindexSee my previous replyMadhivananFailing to plan is Planning to failOriginally posted by balaganapathy.n
 I think this way.Select * from dbemp where empname like '%'+@searchtext+'%'  ORDER BY CHARINDEX( @searchtext, empname, 0) ASCbalaganapathy n.Anything you can imagine is real.
 
 
 
  MadhivananFailing to plan is Planning to fail |  
                                          |  |  | 
                            
                       
                          
                            
                                    | sidharth.banyalStarting Member
 
 
                                    11 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 08:30:25 
 |  
                                          | Sorry it is not working some records are still behind those which are having @searchtext in begining.I tried both solutions but they are not working in desired waySidharth Banyal |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 08:46:32 
 |  
                                          | quote:Try thisSelect * from dbemp where empname like '%'+@searchtext+'%' order by charindex(@searchtext ,empname ),empnameMadhivananFailing to plan is Planning to failOriginally posted by sidharth.banyal
 Sorry it is not working some records are still behind those which are having @searchtext in begining.I tried both solutions but they are not working in desired waySidharth Banyal
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | sidharth.banyalStarting Member
 
 
                                    11 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 08:51:53 
 |  
                                          | Sorry dear its still not working now it sorting records on empname.Sidharth Banyal |  
                                          |  |  | 
                            
                       
                          
                            
                                    | sidharth.banyalStarting Member
 
 
                                    11 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 08:55:18 
 |  
                                          | Its not sorting it at all records are stil in same order as they were before in table. is there some other way to do this charindex and patindex not working at all.Sidharth Banyal |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 08:59:43 
 |  
                                          | quote:What is the datatype of empname?Post some sample data with expected resultMadhivananFailing to plan is Planning to failOriginally posted by sidharth.banyal
 Its not sorting it at all records are stil in same order as they were before in table. is there some other way to do this charindex and patindex not working at all.Sidharth Banyal
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 09:05:01 
 |  
                                          | What is about this? order by case when ltrim(rtrim(Empname)) like @searchtext+'%' then 1     when ltrim(rtrim(Empname)) like '%_'+@searchtext+'_%' then 2     else 3end No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | sidharth.banyalStarting Member
 
 
                                    11 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 22:39:40 
 |  
                                          | order by case when ltrim(rtrim(Empname)) like @searchtext+'%' then 1     when ltrim(rtrim(Empname)) like '%_'+@searchtext+'_%' then 2     else 3endThis is the perfect solution. Its working fine.DataType of empname is Varchar.Thanks to all of you.Thanks a lotSidharth Banyal |  
                                          |  |  | 
                            
                       
                          
                            
                                    | sidharth.banyalStarting Member
 
 
                                    11 Posts | 
                                        
                                          |  Posted - 2010-01-07 : 23:34:29 
 |  
                                          | select * from dbemp  order by case  when Charindex(@searchtext,empname ) = 0 then 255 else Charindex(@searchtext,empname)end,empname also works fine. Problem with charindex is that it returns 0 when no match is found and record are ordered before those which have match.So, we need to put records with charindex 0 behind others. 255 is length of empname column.But, soluion provided by webfred is a lot better.Sidharth Banyal |  
                                          |  |  | 
                            
                       
                          
                            
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2010-01-08 : 01:20:05 
 |  
                                          | Glad to help  Edit: instead of "Glad I could help" now "Glad to help" because I think "Glad I could help" is wrong... No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  | 
                            
                            
                                |  |