Skip to content

If the search word includes %... #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kmvan opened this issue Sep 11, 2021 · 3 comments
Closed

If the search word includes %... #65

kmvan opened this issue Sep 11, 2021 · 3 comments
Labels

Comments

@kmvan
Copy link

kmvan commented Sep 11, 2021

const { format, escape } = require('sqlstring')
const searchWord = 'word%' // Only want to search end of like "abcword%".
const sql = format(
  `
SELECT * FROM ?? WHERE ?? LIKE ?
`,
  ['table', 'content', `%${escape(searchWord)}`]
)
console.log(sql)
// SELECT * FROM `table` WHERE `content` LIKE '%\'word%\''

Expected output

// SELECT * FROM tableWHEREcontentLIKE '%word\%'

Actual output

// SELECT * FROM tableWHEREcontentLIKE '%\'word%\''

Any idea?

@dougwilson
Copy link
Member

Escape function only escapes a value into a sql value. You need to escape the various characters used in LIKE if you don't want them to be special just like with REGEXP.

@kmvan
Copy link
Author

kmvan commented Sep 11, 2021

If the search word includes ? question mark...

@dougwilson
Copy link
Member

I'm not sure what your comment means. The LIKE statement does not assign any special meaning to a question mark, only percent signs and underscores.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants