Ad

Select Substring Of Column In Sequelize

- 1 answer

I am working with sequelize. I want to select a substring in my where clause like this in SQL SUBSTRING(description_column, 1, 200). I am selecting by page, pagesize and limiting description_column to length of 200. I don't know if I can achieve it with raw query since I want pagination which findAll and FindAndCountAll offers which I want to use. How can I achieve this? The only substring I find in sequelize works this way

[Op.substring]: 'hat' // LIKE '%hat%'

Ad

Answer

You can use Sequelize.literal() to use the Standard SQL functions:

const options = {
  attributes: [
    'id',
    sequelize.literal('SUBSTRING(description_column, 1, 200) as description_column')
  ]
}
MyTable.find(options).success(success);
Ad
source: stackoverflow.com
Ad