忽略ORDER BY子句中的引号,数字,空格等

时间:2015-10-28 18:04:44

标签: sql postgresql sorting

我使用ORDER BY title ASC按标题对产品进行排序,然后获取:

"Some" title       // "
1 More title       // 1 
Another title      // A
Third title        // T
Yet another title  // Y

我想忽略引号和其他非字母字符作为查询的一部分,这样它就会产生:

Another title      // A
1 More title       // M
"Some" title       // S
Third title        // T
Yet another title  // Y

是否可以使用Postgres删除此内容或作为查询的一部分进行预处理,或者我是否需要其他列?

更新

效果很好:LOWER(regexp_replace(title, '[^[:alpha:]]', '', 'g')) ASC

此处有更多选项:http://www.postgresql.org/docs/current/interactive/functions-matching.html#POSIX-CLASS-SHORTHAND-ESCAPES-TABLE

3 个答案:

答案 0 :(得分:1)

使用替换

 ORDER BY replace(title, '"', '') asc

更多进步

ORDER BY regexp_replace(title, '[^a-zA-Z]', '', 'g')

答案 1 :(得分:1)

您可以使用已替换"的字符串排序以清空字符串。数字字符也一样。

SELECT *
FROM your_table
ORDER BY REPLACE(REPLACE(title, '"', ''), '1', '') ... ASC

使用regex_replace

SELECT *
FROM your_table
ORDER BY regexp_replace(title, '[^A-Za-z]', '', 'g') ASC;

答案 2 :(得分:1)

一种方法是使用regexp_replace()

order by regexp_replace(title, '[^a-zA-Z]', '', 'g')

您也可以使用:

order by regexp_replace(title, '[^[:alpha:]]', '', 'g')

对于非ASCII字符集,这更安全。