Microsoft KB Archive/307514

= A LIKE statement does not return any data with Oracle character data type =

Article ID: 307514

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft ADO.NET 2.0
 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 2.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q307514



This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System
 * System.Data
 * System.Data.OleDb



SYMPTOMS
When you use Microsoft OLE DB Provider (MSDAORA) or the OLE DB .NET Data Provider for Oracle, if you use a LIKE statement to search fixed-length character types, Oracle does not find any rows if their padding is different.



CAUSE
If you type a value into a fixed-length field that is shorter than the length of that field, the value is padded with trailing spaces to fill the fixed length. When Oracle matches values for fixed-length fields, it matches the entire length of the string, including any trailing spaces.

NOTE: This behavior is not specific to ADO.NET and also occurs when you use Microsoft ActiveX Data Objects (ADO).



RESOLUTION
To resolve this problem, always use the percentage (&quot;%&quot;) wildcard character when you use LIKE statements to compare values in fixed-length fields in an Oracle database. For example: cmd.Parameters.Add(&quot;@p1&quot;, OleDbType.Char, 3).Value = &quot;a%&quot;



Steps to Reproduce the Behavior
  In Oracle, create a table named TestTable as follows: Create Table TestTable (c1 char(3));   Insert the value 'a' into the table as follows: Insert into TestTable c1 values('a');  Open Microsoft Visual Studio .NET or Visual Studio 2005. Create a new Windows Application project in Visual Basic .NET. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.</li> Place a Command button on Form1, and change its Name property to btnTest .</li>  Use the Imports statement on the System and System.Data namespaces so that you are not required to qualify declarations in those namespaces later in your code. Imports System Imports System.Data Imports System.Data.OleDb </li>  Copy and paste the following code in the code window after the &quot;Windows Form Designer generated code&quot; region: Private Sub btnTest_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnTest.Click Dim sConnectionString As String _ = &quot;Provider=MSDAORA.1;User ID=scott;password=tiger;&quot;_ &quot;Data Source=myOracleServer;Persist Security Info=False&quot; Dim mySelectQuery As String _ = &quot;SELECT * FROM TestTable where c1 LIKE ?&quot; Dim myConnection As New OleDbConnection(sConnectionString) Dim myCommand As New OleDbCommand(mySelectQuery, myConnection) 'To resolve this problem, change the parameter 'in the next line to use &quot;%&quot; wildcard character. myCommand.Parameters.Add(&quot;@p1&quot;, OleDbType.Char, 3).Value = &quot;a&quot; myConnection.Open Dim myReader As OleDbDataReader = myCommand.ExecuteReader Dim RecordCount as Integer Try While myReader.Read RecordCount = RecordCount + 1 MessageBox.Show(myReader.GetString(0).ToString) End While If RecordCount = 0 then MessageBox.Show(&quot;No data returned&quot;) Else MessageBox.Show(&quot;Number of records returned: &quot; & RecordCount) End If   Catch ex As Exception MessageBox.Show(ex.ToString) Finally 'Always call Close when done reading. myReader.Close myConnection.Close End Try End Sub </li> Save your project.</li> On the Debug menu, click Start, and run your project.</li> Click the button. Notice that no data is returned.</li>  Change the parameter to use &quot;%&quot; wildcard character as follows: myCommand.Parameters.Add(&quot;@p1&quot;, OleDbType.Char, 3).Value = &quot;a%&quot; </li> Run the project again. Notice that the data is returned successfully.</li></ol>

<div class="references_section">