0

テーブルにエントリがあります

12-345678
123-456-789
123456789

UIテキストボックスで、誰かが次のように入力した場合:  - (two spaces then one dash).

出力は次のようになります。

12-345678   

UIテキストボックスで、誰かが次のように入力した場合:12-

出力は次のようになります。

12-345678

UIテキストボックスで、誰かが次のように入力した場合:12

12-345678
123-456-789
123456789

これを行うための私の選択クエリは

select column1,column2 from table1 where column1 like '%textbox1.text%';

textbox1.text->UIのテキストボックス名。

前のすべて-例:返される必要があります:

if you type 12-, return output should be 12-345678 alone,
if you type 123-, then the return output should be 123-456-789 alone.
if you type - (only dash, no space on the front , then the return output should be 12-345678 and 123-456-789 .

ただし、いくつかの条件で失敗します。スペースを計算し、SQLでクエリを直接変更する方法はありますか?

4

2 に答える 2

1

私がこれを正しく理解している場合: 先頭にスペースがある場合は、それらを に変換する必要があります_。先頭にスペースがない場合は、単に追加できます%

<space><space>-  '__-%'
12-              '12-%'
12               '12%'
123-             '123-%'

ただし、これはパターンに適合しないため、特殊なケースです。

-                '%-%'

1行の修正が必要だと思いますが、おそらくすべてを1つのにまとめることができますがiff()、可能であればそれは避けます.

編集して追加

C#:

string arg = theTextbox.Text;
arg = arg.Replace("'", "''"); // avoid SQL injection attack

arg = arg.Replace(" ", "_"); // space = single character wildcard = '_'

if (arg.StartsWith("-")) // special case
    arg = "%-";

string sqlStatement = string.Format("SELECT column1, column2 " +
             "FROM table1 WHERE column1 like '{0}%', arg);
于 2012-08-08T21:53:41.850 に答える