yn1043
yn1043

Reputation: 588

How can I use Like operator in mssql(node.js)

I'm using mssql(node.js). I want to execute SQL having LIKE.

But I can't find out in below page https://www.npmjs.com/package/mssql#input-name-type-value

my code is this.But it doesn't work(result is 0 record)

mssql.connect(config, function(err) {

var request = new mssql.Request();
request.input('name',mssql.NVarChar,name);

request.query("SELECT * FROM Table WHERE name LIKE '%@name%',function(err,data){
//
}

Upvotes: 0

Views: 3080

Answers (2)

Fenix
Fenix

Reputation: 2391

You can use prepared statement like "select * from t where '%' + @param + '%'".

For example:

mssql.connect(config, function(err) {

var request = new mssql.Request();
request.input('name', mssql.NVarChar,name);

request.query("SELECT * FROM Table WHERE name LIKE '%' + @name + '%'", function(err,data){
//
}

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521168

Try representing the entire LIKE expression with a parameter, and then bind it using a concatenation, e.g.

var name = 'Yoshihide Nishimoto';

request.query("SELECT * FROM Table WHERE name LIKE ?", '%' + name + '%',
    function(err, data) {
        // code here
    }
)

Upvotes: 1

Related Questions