简体   繁体   中英

Find a user_id from string where id is one digit or 2 digit using SQL Server query

Here is my C# code

private DataTable bindTrainingAttendeedGridView(string condition)
{
    DataTable dtGrid = new DataTable();
    con.ConnectionString = constr;

    string query = "select training_id,CONVERT(varchar(10),training_date,101) AS Training_date,topic,trainee from tbl_training_attendee where 1=1 " + condition;

    SqlCommand cmd = new SqlCommand(query, con);
    SqlDataAdapter sqladapter = new SqlDataAdapter(cmd);
    sqladapter.Fill(dtGrid);

    if (dtGrid.Rows.Count <= 0)
    {
        Label1.Text = "No Matching Training taken Entries Found, Thank You.";
    }
    else
    {
        Label1.Text = "";
    }

    //con.Open();
    return dtGrid;
}

protected void TrainingAttendenceGridViewBind(string condition)
{
    try
    {
        TrainingAttendeedGridView.DataSource = bindTrainingAttendeedGridView(condition);
        TrainingAttendeedGridView.DataBind();
    }
    catch (Exception er)
    {
        throw er;
    }
}

protected void training_attendence_condition()
{
    string condition = "";

    if (UserDropDownList.SelectedIndex > 0)
    {
        condition += "AND PATINDEX('%61%',trainee)!=0 or(PATINDEX('%,61,%',trainee)!=0 and PATINDEX('%,61%',trainee)!=0 and PATINDEX('%61,%',trainee)!=0 ) 
    }

    TrainingAttendenceGridViewBind(condition);
}

In training_attendence_condition , I can write a query for getting user_id for that row but it can show only two digit number records and when take one digit number then it can return that number with second digit. So please help to get both one or two digits

在此处输入图片说明

试试这个SQL查询,不需要training_attendence_condition,你可以直接写给定的查询:

select training_id,CONVERT(varchar(10),training_date,101) AS Training_date,topic,trainee from tbl_training_attendee where CONCAT( ',',trainee,',') like CONCAT( '%,','61',',%' )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM