我使用 select pk_customer_no from customer order by pk_customer_no
对数据进行排序
带“-”的代码,没有分组并按字母排序,看来sql只是忽略它并按第三个字母排序。
如何按“-”排序?
最佳答案
排序时会忽略“-”字符。
如果您想将带有“-”的单词放在前面,可以通过将“-”替换为“0”(零)来使用 order。
select t.pk_customer_no as rep from (
values ('YH'), ('YHC'), ('Z-CH'), ('Z-CHE'), ('ZCM'), ('Z-CP'), ('Z1'), ('ZHT'), ('ZLA'), ('Z-JP'), ('ZLENO')
) as t (pk_customer_no)
order by replace(t.pk_customer_no, '-', '0')
如果您想将带有“-”的单词放在末尾,可以通过将“-”替换为“Z”来使用 order。
select t.pk_customer_no as rep from (
values ('YH'), ('YHC'), ('Z-CH'), ('Z-CHE'), ('ZCM'), ('Z-CP'), ('Z1'), ('ZHT'), ('ZLA'), ('Z-JP'), ('ZLENO')
) as t (pk_customer_no)
order by replace(t.pk_customer_no, '-', 'Z')
关于sql - 特殊字符的 order by 规则是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72477055/