Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Searching keywords in MS SQL

I need to be able to search for keywords in an MS SQL database which I'm able to do except the keywords are encapsulated in brackets which I would like to include in my search. For example, here is how I'm doing it now...

$sqlquery="SELECT * FROM HISTORY WHERE DESCRIPT LIKE '%$wo_id%';";
$results=mssql_query($sqlquery);
$row=mssql_fetch_array($results);

where wo_id is the keyword I'm searching for. The problem with this is, the number could come up in the results from a row that I don't want to include. The problem I have run into is the table the data is stored in doesn't have a column for the id number. The number is in the description like this [4567]. How do I do the above code but also include the brackets. I've tried escaping them or setting them in a variable first but it isn't working for me.

Thanks.
0
schnazzer
Asked:
schnazzer
  • 4
  • 3
1 Solution
 
hclgroupCommented:
You have to escape it like this

select * from Customers where Companyname like '%[[test]]%'

You have to replace [ with [[ and ] with ]]
0
 
schnazzerAuthor Commented:
just as a test, here is what I tried...

SELECT * FROM HISTORY WHERE DESCRIPT LIKE '%[[100]]%'

its still pulling more than what I'm looking for. in fact, in my query, I'm pulling data that doesn't even contain [100] in it.

for example..here is a row that comes up

CANCELLED: WS#[]
0
 
schnazzerAuthor Commented:
ok, so I'm doing something wrong.... I'm getting results based on individual characters in my query, for example, doing a search for [1024] lists items similar to my example below...

CANCELLED: WS#[]
CANCELLED: WS#[1]
CANCELLED: WS#[2]
CANCELLED: WS#[4]
CANCELLED: WS#[1024]
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
hclgroupCommented:
My mistake

replace [ with [[] and ] with []]


E.g. SELECT * FROM HISTORY WHERE DESCRIPT LIKE '%[[]100[]]%'
0
 
hclgroupCommented:
My mistake again

My mistake

replace [ with [[] and ] with ]


E.g. SELECT * FROM HISTORY WHERE DESCRIPT LIKE '%[[]100]%'
0
 
schnazzerAuthor Commented:
Interesting,

Thanks, that worked....
0
 
hclgroupCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now