Compare words and get exact match

Now it's complicated ... I hope I can explain it well.

I have a table that calls it, and I need to compare it with the word i and get an exact match.

Now I say that it is difficult because I tried this query and gave me a set of names. It contained a word that was an exact coincidence, as well as words that looked like ...

this is what i did:

DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(); connection.ConnectionString = ConfigurationManager.ConnectionStrings["xyz "].ConnectionString; connection.Open(); SqlCommand sqlCmd = new SqlCommand("SELECT Names FROM Machines WHERE Name Like '%' + @userName + '%'", connection); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlCmd.Parameters.AddWithValue("@userName", userName); sqlDa.Fill(dt); connection.Close(); 

Now I want ... for example.

if the database has three words

ab123

XYZ123

pqc1238

Now, if the word is 123, it should return abc123 and xyz123 if c123, then return abc123 and NOT pqc1238 and xyz123

if 238, then just return pqc1238 ....

I hope I was clear enough ... believe me, I didn’t receive it for the first time and

any sentences are better to push exact matches of words. coz LIKE is not enough.

thanks

+4
source share
7 answers

It seems to me that you are looking for a line ending with @userName.

 SqlCommand sqlCmd = new SqlCommand("SELECT Names FROM Machines WHERE Name Like '%' + @userName", connection); 
+1
source

I would not call this correspondence EXACT, but if you are looking for words ending with a given word, try:

 "SELECT Names FROM Machines WHERE Name Like '%' + @userName" 

no second%

+2
source

This is almost correct for you, but you do not understand the wildcard%.

In SQL, the% wildcard will match any string of any length. You want this in the beginning because you don't need the driving parts.

However, when you put it at the end of your LIKE matching string, it will allow a match if something happens after your string.

So let's take your example

ab123

XYZ123

pqc1238

Now, if the word is 123, it should return abc123 and xyz123, if c123, then return abc123 and> NOT pqc1238 and xyz123

if 238, then just return pqc1238 ....

You have% 123% you say: "I need all the lines that start with anything, then" 123 "and then end with anything." This will match any string that contains “123” at any point.

What you want to say is "I want all lines to start with anything, then" 123 "and then end." So this is% 123.

Finally, to get an EXACT match with a similar one, just leave all the wildcards away.

This page has a pretty good overview of SQL wildcards.

+2
source

From what I understand, it seems you only need a line ending with a search pattern, so all you have to do is omit the last “%” of your choice.

 SqlCommand sqlCmd = new SqlCommand("SELECT Names FROM Machines WHERE Name Like '%" + @userName + "'", connection); 
+1
source

It reads as if you want the form command to select names from machines, where names like "% xxx", where "xxx" is your word

If this is the case, and @userName is your word, you need something like

 SqlCommand sqlCmd = new SqlCommand("SELECT Names FROM Machines WHERE Name Like '%' + @userName", connection); 
+1
source

Why aren't you trying to use LINQ? Do something like:

 var query = from c in db.Machines where c.Name.EndsWith("123") select c.Names; 

Hope this helps.

+1
source

It seems to me that you want all entries to end with a specific word. In this case, you just need to remove the last "%" that you add to the request.

 SqlCommand sqlCmd = new SqlCommand("SELECT Names FROM Machines WHERE Name Like '%' + @userName", connection); 

Hope you need

+1
source

Source: https://habr.com/ru/post/1299759/


All Articles