OracleでCHAR定義されたカラムの情報を、ADO.NET経由で検索した際にうまく結果が返らないという相談を受けたのですが……。たしかOracleのCHAR型って、勝手に後ろに空白が入って固定長フルサイズまでパディングされたような。そうすると、厳密な一致という条件で検索された場合、LIKE文で前方一致などによる検索をしないと引っかからないような気がします。というか、固定長文字列にしないといけないかどうか、から本当は検討すべきなんでしょうけど……。
このあたりが参考になるでしょうか??(既にMicrosoft のKBページが消えていたので、Internet ArchiveのWayback Machineのリンクに差し替えます)。
こちらでわかりやすく実例を示してもらってますが、Oracleでの検索だと、CHARに対する検索だと空白は良くも悪くも無視してくれるようなのですが、Oracle外からだとだめなんですかね。
その辺も把握した上でDB設計からやらないと、どこかにしわ寄せが出てしまいますね。
参考: KB 307514 [PRB] LIKE ステートメントが Oracle 文字データ型のデータを返さない
2010/03/14の記録から転記。
A LIKE statement does not return any data with Oracle character data type
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 ("%") wildcard character when you use LIKE statements to compare values in fixed-length fields in an Oracle database. For example:
cmd.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a%"