.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Sort query results by the number of characters matched

Posted By:      Posted Date: September 30, 2010    Points: 0   Category :Sql Server


I have a query that takes a multi-word search string and parses it to spit out a LIKE (with AND or OR) condition that I then insert into my dynamic query.

Example of my parsing:

    'cheese chocolate cake'))),
                                ' ', ' þ')
                                , 'þ ',
                                    '), ' þ'
                    , 'þ'),
                    '%' OR CakeName LIKE '%') + '%')' 

Say for example that a user searches for the string "cheese chocolate cake". My parsing would result in the following string:

(c.CakeName LIKE '%CHEESE%' 
OR CakeName LIKE '%CAKE%')

What I wan to do is sort the results based on the number of characters that were actually matched. So, if c.CakeName contained the following:


  • Cheese Cake
  • Chocolate Cake
  • Chocolate Cheese Cake
  • Lemon Cake

The results would be sorted thusly:

  1. Chocolate Cheese Cake
  2. Chocolate Cake
  3. Cheese Cake
  4. Lemon Cake

  5. View Complete Post

More Related Resource Links

When I save query results to a csv file I get spurious characters at start of file


If I edit a csv file created from a query result there are three spurious characters at the start of the file.

If I save the file using ansi encoding (clicking on the down arrow next to save button and selecting ansi) the characters do not appear, which is what I want.

Is there any way to default to ansi encoding when saving query results to a csv file?

Query a Table with Japanese Characters returns always 0 results


Hi all, I wonder if you could give me a hand on this please...

I've a table (lets say with 2 columns: ID int, Vendor Name nvarchar(255)) with some records with Japanese Characters. 

When I try to query that table for the specific rows with Japanese string I always get 0 results.


Table example:


ID Vendor Name

1 Bruno Ferreira

2 ??????????


Query Example:

select * from myTbl where [Vendor Name] collate <<collations name>> = '??????????'


Result always nothing...


I've tried with all the possible available collations but with no luck

collations I've tried:











Linq query results to List collection



I have a GridView in my page. I want to use below code in code beind. query nothwing DB using linq . add linq query result to new list and then bind list as gridview Datasource

I use below code . But I don't know how to add results to List? Could any one tell me how !

Public Class OrderCity
        Private _name As String
        Private _city As String
        Public Property name As String
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value

            End Set
        End Property
        Public Property city As String
                Return _city
            End Get
            Set(ByVal value As String)
                _city = value

            End Set
        End Property
    End Class
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim dbe As New SFFAV1DataContext
        Dim latestadsDs = From p In dbe.Products _
                            Select New With {p.Title, p.ProducedCity} _
                             Take (2)

        Dim FilteredList As New List(Of OrderCity)()
        For Each result In latestadsDs
            Dim Menedfcou = Nothing
            FilteredList.Insert(0, Menedfcou)

E-mail query results

I have the following piece of code that returns nothing when there is only one row that fits the criteria. If there are two or more rows that match the criteria it works. Any idea what is wrong?? Declare @drugID varchar(4000),@lineresult varchar(4000), @SEND VARCHAR(256), @RECP VARCHAR(256), @TITLE VARCHAR(256), @BODY VARCHAR(4000), @SERVERNAME VARCHAR(256) set @drugID = '' declare r_cursor INSENSITIVE CURSOR FOR SELECT AHI_DRUG_DOSE.Drug_Display_Name+ ' ' +convert(varchar(10),AHI_CAB_EVENT.Station_Id)+ ' ' +convert(varchar(25),AHI_CAB_EVENT.Event_Dttm)+ ' ' + AHI_CAB_EVENT.Event_Type+ ' ' + convert(varchar(10),AHI_CAB_EVENT.Trans_Qty)+ ' ' +convert(varchar(25),AHI_CAB_EVENT.Site_Patient_Id)+ ' ' +AHI_CAB_EVENT.Pat_Name+ ' ' +AHI_CAB_EVENT.Misc1 FROM AHI_DRUG_DOSE AHI_DRUG_DOSE INNER JOIN CRX_DATA.dbo.AHI_CAB_EVENT AHI_CAB_EVENT ON AHI_DRUG_DOSE.Drug_Dose_Id = AHI_CAB_EVENT.Drug_Dose_Id WHERE (AHI_CAB_EVENT."Station_Id" = 'OPS' OR AHI_CAB_EVENT."Station_Id" = 'OR1' OR AHI_CAB_EVENT."Station_Id" = 'OR2' OR AHI_CAB_EVENT."Station_Id" = 'PACU') AND AHI_CAB_EVENT."Event_Type" = 'WASTE' AND AHI_CAB_EVENT."Misc1" <> 'Partial Dose' AND AHI_CAB_EVENT."Event_Dttm" >= get

Search query returning 0 results

I'm using a query which returns 0 results but was working some weeks ago.  I checked the code and nothing has changed since then.  I created a Search Center Site on another port and using the same terms I get many results there.  I checked my scopes and they are all right. Nothing changed there either. Could someone help me figure out what may be the problem in my query.  Or maybe it's my Scopes/Content Sources configuration. SELECT URL, FileName, Title, Description, Rank, Size, HitHighlightedSummary, Path FROM SCOPE() WHERE freetext(defaultproperties, ' +légumes')  AND ( "scope" = 'Scope - My Portal - All Site - fr'  )    ORDER BY Rank DESCDaniel Siconnelli There is neither good nor evil... only mogwais and gremlins

Need help in CAML Query - Sort Operation

hi I have a list column "DisplayOrder" in which i have integer values, i am using this query <Query><OrderBy><FieldRef Name="DisplayOrder" Ascending="True" /></OrderBy></Query> this returns me listcollection 1 10 15 2 it is considering the colums as string, can i convert this column to integer before sorting? i want this result 1 2 10 15

Add mobile number to search results in People Finder

Hi, Could someone tell me if it is possible to add content to the People Finder search results, namely the users mobile number. If if is possible could you tell me how or point me in the direction of a how to? ThanksDavid Hodgson Add me on Twitter and LinkedIn www.infinitygroup.co.uk

How to tell repeater to wrap text after certain number of characters for a particular column ?

I have a column in my repeater which allows as many as 255 characters and causes the page contents pushed away to the right if any of the record has all 255 characters in that column. How can I tell it to wrap the text after 50 characters ?Thanks,

Number of query values and destination fields are not the same

If RadioButton1.Checked = True Then rbtn = 1 Else rbtn = 2 End If ' Our insert command strSQLQuery = "INSERT INTO tblJobs " _ & "(filingDate, filingDay, reportDate, reportTime, company, jobsite, location, duration, DrugTestReq, foreman, instructions, jobSteward, nPlumbers, nFitters, nWelders, nTradesmen, nPlAppr, nFitAppr, nServTech, nHelpers) " _ & "VALUES (@DTValue, @TextDay, @DTValue2, @TextValue, @TextValue2,@TextValue3,@TextValue4,@TextValue5,@rbutton,@TextValue6.@TextValue7,@TextValue8,@NumVal,@NumVal2,@NumVal3,@NumVal4,@NumVal5,@NumVal6,@NumVal7,@NumVal8)" ' Create new command object passing it our SQL insert ' and telling it which connection to use. objCommand = New OleDbCommand(strSQLQuery, objConnection) ' Add parameters that our SQL command takes: 'radio button 'OleDbType. objCommand.Parameters.Add(New OleDbParameter("@rbutton", OleDbType.LongVarChar)) '- objCommand.Parameters.Add(New OleDbParameter("@DTValue", OleDbType.Date)) objCommand.Parameters.Add(New OleDbParameter("@TextDay", OleDbType.LongVarChar)) objCommand.Parameters.Add(New OleDbParameter("@DTValue2", OleDbType.Date)) objCommand.

Search results page showing random characters instead of item's description

I have an email-enabled document library that a few Japanese people email PDF files into (all external to the company). When I search for any PDF file sent from particular email addresses, the search results page displays random characters instead of the item's description. Here is an example: From Screen shots These characters don't appear for all email senders, just some of them. Our MOSS site is in English and we don't have any language packs installed. The PDF file contents are also in English. Can anyone point me towards a solution for this?

how can I detect query results

Im trying to get the number of result in a select query in a database...how can I get the number(row) of result from a ExecuteReader() function?

Sort Query

Hi , I have the following table TableData ColName           DataType DatasetID          int FieldID               int RowID               int FieldValue         varchar I have a scenario to Sort tha data in this table, this table stores data in  a dataset where column and its values are stored as rows in the table. The reason to have such a table is the no of columns in each dataset could vary in runtime. This generic table will hold dataset with any no of rows and columns. Each row is identified by ROwID. The data from this table will be flattened into rows in the UI. Basically I need a query to sort data in ths table based on FieldValue for each rows. Thanks, Ajeeth Ajeeth Kumar.A, LionBridge Technologies

Dynamic SQL query cannot store more than 4000 characters even with NVARCHAR(MAX)

Hello, everyone. I am having a rather strange problem when building a dynamic query in SQL Server. I declare a variable of NVARCHAR(MAX) type, and build some queries into it, running them later with sp_executesql. One or two of these queries are so large that they surpass the 4000 char limit that, supposedly, NVARCHAR(MAX) overcomes. However, I still am having the classic problem of storing the whole query inside the variable! I use SQL Server 2008. All the documentation online I checked states that the solution is just using NVARCHAR(MAX) and the problem should not happen. How can this keep happening? Cheers, Ivo Pereira IT Consultant PortugalComputerDoc

Analysis Service Oracle Number inconsistent Data Type for TABLE or Named Query

Dear Gurus, I'd VERY OLD PROBLEM. And I believe it addressed since 2006. When I design DataSource Views from Oracle Data Source. I found it return different oracle number data type for TABLE or NAMED QUERY   Provider Data Type Column Data Type Data Source View Data Type Oracle OLE DB Provider (OraOLEDB.Oracle.1) Table Number System.Int64   View Number System.Decimal   Named Querey Number System.Decimal Microsoft OLE DB Provider for Oracle (MSDAORA.1) Table Number System.Double   View Number System.Double   Named Query   1 System.Int64   Named Query   1.1234 System.Int64 Althought I know I can fix IT via MANUALLY EDIT DATASOURCE VIEW XML SOURCE. But I don't think this is a better solution. Is anybody have ideas ?  Wilson

Query Designer - Pane - Results grayed-out?

In Query Designer, right click, select pane -- the option for Results is unavailable (grayed-out)?

How to extend this query to write the results back to a database?

FOr this query I have four tables I would like to access. SELECT StockA, SUM(Score) AS Total FROM dbo.ScoresNew_LongPrice WHERE [When] >= @StartTime AND [When] < @EndTime GROUP BY StockA ORDER BY SUM(Score) DESC _LongPrice, _ShortPrice, _LongVolume, _ShortVolume I would like to improve the above query to return not just the results from LongPrice, but four columns, of sum(score), from each table, if possible. And, if that is possible, I would also like to have the query then update another table with its results.  Not a new table, but just updated to an existing table. Anyway, I can do some stuff in T-SQL but this seems over my head.  Any help would be appreciated. (The four tables all have identical structures)

"Results Per Page" and "Paging Number" not retained when set by user

When a user selects the "results per page" to another number other than the default OR a user advances to a higher page number in the GridView other than page "1".  The settings are not retained when going between the "list.aspx" and "edit.aspx".  The user has to reset the "results per page" and scroll through the paging to get back to the "page number" they were at when they decided to edit the data. List --> Edit --> List Is it possible for the application to remember where the user was at after performing the operation on the edit page and return to that exact page number/location AND for it to also remember how many "results per page" the user selected?  
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend