[英]Sort strings with custom order in Oracle SQL
Problem :问题:
We first define a custom character order in the following way:我们首先通过以下方式定义自定义字符顺序:
A < ... < Z < a < ... < z < 0 < ... < 9 < <
capitals lowercase digits printable special characters space
Special characters' order is not specified, so, they can be compared arbitrarily.特殊字符的顺序没有规定,可以任意比较。
Given a table INPUT
with one VARCHAR2
column, sort its content (strings containing only characters listed in order definition) in lexicographic order, using the character order defined above.给定一个包含一个
VARCHAR2
列的表INPUT
,使用上面定义的字符顺序,按字典顺序对其内容(仅包含顺序定义中列出的字符的字符串)进行排序。
Question: how can one implement such sorting in pure Oracle SQL (without using PL/SQL and undocumented functions)?问题:如何在纯 Oracle SQL 中实现这种排序(不使用 PL/SQL 和未记录的函数)?
It looks like Oracle DB doesn't have a feature to use such an order directly in ORDER BY
.看起来 Oracle DB 没有直接在
ORDER BY
使用此类订单的功能。
You could use translate()
:您可以使用
translate()
:
order by translate(col,
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
)
This replaces the characters that you care about with the "natural" ASCII ordering of the characters.这将用字符的“自然”ASCII 顺序替换您关心的字符。
You can use a collation at the column level.您可以在列级别使用排序规则。 Collations specify how
VARCHAR2
values are compared and sorted.排序规则指定如何比较和排序
VARCHAR2
值。
See Column Level Collation or Statement Level Collation .请参阅列级排序规则或语句级排序规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.