Use LIKE or LTRIM - SQL

2

I am working with oracle and C # in Visual Studio 2015 and I have this query

"SELECT * FROM TCTERCEROS WHERE TR_TERCERO = '" + TBNit.Text + "'"

As I can apply a LIKE or LTRIM, TBNit.Text is what is said in the text box to see for example 123 but in the BD this is 000000123

    
asked by Saul Salazar 10.08.2018 в 15:35
source

3 answers

1

Well the LIKE is used with the sign % to capture the entries, that is:

This is if you want to match everything

"SELECT * FROM TCTERCEROS WHERE TR_TERCERO LIKE '%" + TBNit.Text + "%'"

This is in case you want to match at the beginning

"SELECT * FROM TCTERCEROS WHERE TR_TERCERO LIKE '" + TBNit.Text + "%'"

This is in case you want to match at the end

"SELECT * FROM TCTERCEROS WHERE TR_TERCERO LIKE '%" + TBNit.Text + "'"

I'll give you the guide to LIKE in ORACLE to guide you better.

I hope it has served you and marques xD. ReNiceCode ...

    
answered by 10.08.2018 / 15:44
source
1

The accepted answer solves your problem, however queries with chain concatenation are not recommended.

It is advisable to use parameterized queries instead of concatenating the strings. It would be something like this:

Creating a variable in the query: @nombre_variable .

"SELECT * FROM TCTERCEROS WHERE TR_TERCERO LIKE @nombre_variable";

And the way to use it would be something like this:

comando.Parameters.Add("@nombre_variable", suTipoDato).Value = "%" + TBNit.Text + "%";

With this, avoid the SQL injection ...

Just to give a larger example:

using(SqlConnection con = new SqlConnection(...))
{
    string Comando = "SELECT * FROM TCTERCEROS WHERE TR_TERCERO LIKE @nombre_variable";
    using (SqlCommand cmnd = new SqlCommand(Comando, con)
    {
        cmnd.CommandType = CommandType.Text;
        cmnd.Parameters.Add(new SqlParameter() {
                                ParameterName = "@nombre_variable",
                                SqlDbType = SqlDbType.NVarChar,
                                Value = string.Format("%{0}%", TBNit.Text)
                            });

        con.Open();
        using (SqlDataReader dataReader = cmnd.ExecuteReader())
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(consulta);
            dataReader.Dispose();
            da.Fill(dt);
            dtgv.DataSource = dt;
        }
    }
}
    
answered by 10.08.2018 в 21:16
0

Add the TRIMSTART function to the text TBNit.Text. Staying in the following way:

TBNit.Text.TrimStart('0')

I hope it helps you. Greetings.

    
answered by 10.08.2018 в 15:42