Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare case sensitive string data in Sequelize ORM for express.js?

I want to compare some case sensitive string data using sequelize. my string is "HARSH" and in db, it is "harsh" which should not be equal. I'm using where condition to find the data "HARSH" but in the response, I'm getting string data "harsh".

pReadings.user_model.findAll({
    where: {
        firstname: "HARSH"
    }
})
like image 522
Harsh Lodhi Avatar asked Oct 16 '19 11:10

Harsh Lodhi


3 Answers

The collation on the column needs to be ..._bin. It is probably ..._ci, meaning "case insensitive". It was either set that way by default or explicitly.

Please provide SHOW CREATE TABLE for assistance in changing it.

like image 126
Rick James Avatar answered Nov 19 '22 19:11

Rick James


// search case insensitive nodejs usnig sequelize


 const sequelize = require('sequelize');
    let search = "testData"; // what ever you right here
    pReadings.user_model.findAll({
        where: {
            firstname: sequelize.where(sequelize.fn('LOWER', sequelize.col('firstname')), 'LIKE', '%' + search.toLowerCase() + '%')
        }
    })
like image 2
Ajay Prajapati Avatar answered Nov 19 '22 19:11

Ajay Prajapati


Try using the following,

pReadings.user_model.findAll({
    where: sequelize.where(sequelize.fn('BINARY', sequelize.col('firstname')), 'HARSH')
    // SELECT * FROM your_table WHERE BINARY(firstname) = 'HARSH';
})

For more information, check out Querying - Sequelize, under heading "Where > Basics". Good luck!

like image 1
vrintle Avatar answered Nov 19 '22 19:11

vrintle