Knex:如何 trim 结果?

Knex: How to trim the result?

当使用SQL时,查询可以写成:

select RTRIM(dept_id) as dept_id from dept 

除了使用knex.raw,我们可以使用knex原生API实现裁剪吗?

我的SQL服务器版本是2000。

为了 trim 正确设置,您必须同时执行 LTRIM、RTRIM。我建议你在数据库层做 trimming。而不是将数据带到中间层并 trim 在那里进行。当您 trim 在 DB 层时,您也在减少传输到其他层的数据量。

select LTRIM(RTRIM(dept_id)) as dept_id from dept

我们可以使用 knex.raw() 作为部分查询,而不是在 knex.raw()

中更改整个查询
knex.select("*", knex.raw(RTrim(dept_id) as dept_id)).from("dept")