SQL Text Match on Character Stream (wildcards)

5 pts.
Tags:
SQL
SQL statements
Text field
Wildcards
I need to match a text field on zero or more characters. If available, the 'or more' characters need to be in a specific sequence. The % wildcard doesn't quite cut it. For instance, I need to match the name with 'm', 'ma' or 'mar' (but no other character combinations). Is this possible?

Answer Wiki

Thanks. We'll let you know when a new response is added.

Hello Ohreallyus
you can use a combination of wilcards, example:

<pre>
SELECT MYFIELD FROM MY TABLE
WHERE MYFIELD LIKE ‘M%’ OR MYFIELD LIKE ‘MA%’ OR MYFIELD LIKE ‘MAR%'</pre>

This will return everything that starts with M, MA and MAR but also will eb included MS*, MR* and so on.
If instead you want ONLY MAR* just <b>MYFIELD LIKE ‘MAR%’ </b>.
I hope this helps.

Searching a column for M% OR MAR% will give you the same results as searching for M% (and will place additional load on the SQL Server). Are you trying to figure out how to use an input parameter?

Can you post to us your T/SQL code that you have already and we’ll see what we can do with it?

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • MiguelPascoe
    Let me see if I understand what you are asking: you would like something like WHERE NAME = 'M' OR NAME = 'MA' OR NAME LIKE 'MAR%' If your DB has regular expressions, you could use something like WHERE REGEXP_LIKE(NAME, '^M(A(R.*)?)?$' )
    40 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: