oracle中的字符串比较问题

时间:2023-01-25 22:59:34

I am reading basics of oracle and came across strange statement. I don't know how much true it is.

我正在阅读oracle的基础知识,发现了一个奇怪的说法。我不知道有多少是真的。

Statement says

声明说

" String value '2' is greater than String value '100'. Character '1' is less than Character '10'. "

“字符串值‘2’大于字符串值‘100’。字符“1”小于字符“10”。”

Kindly throw some light on above topic. I understand that internally comparison must be happening using ASCII values. I am seeking some good logical explanation.

请就上述问题阐明一些观点。我理解内部比较必须使用ASCII值进行。我在寻求一些合理的解释。

2 个解决方案

#1


5  

It means that numbers treated as strings are not sorted in numerical order but in lexical order, the same way words are sorted in a dictionary. That is, characters are compared one at a time from the left side.

这意味着,作为字符串处理的数字不是按照数字顺序排序的,而是按照词汇顺序排序,就像字典中对单词排序一样。也就是说,每个字符都是从左边来比较的。

In your first example, "2" is greater than "100" because the '2' is compared to the '1' and found to be bigger. Compare this to the ordering of "C" and "BAA" in a dictionary.

在第一个例子中,“2”大于“100”,因为“2”与“1”比较,发现它更大。与字典中“C”和“BAA”的顺序相比。

In your second example, "1" is less than "10" because the "1" fully matches the "1" in the left side of "10", but the "10" has characters following the match. Therefore it is greater. Again, compare this to the ordering of "B" and "BA" in a dictionary.

在第二个示例中,“1”小于“10”,因为“1”与“10”左边的“1”完全匹配,但“10”在匹配后具有字符。因此,更大。同样,将它与字典中“B”和“BA”的顺序进行比较。

#2


0  

You are exactly correct in assuming that they are sorted by ASCII values - this is called an alphabetic sort. The strings are sorted not as numeric values but as text.

您完全正确地假设它们是按ASCII值排序的——这被称为字母排序。字符串不是按数字值排序的,而是作为文本排序的。

The alphabetic sort compares the values position by position. When comparing the string '2' with the string '100' it starts by comparing '2' with '1'. '2' comes after '1' (the ASCII values of '2' is greater than the ASCII value of '1') alphabetically so the comparison stops so '100' will be listed before '2' in an alphabetic sort. This is exactly equivalent as comparing 'b' to 'azz' - since 'a' comes before 'b', 'azz' will be sorted before 'b'.

字母排序按位置比较值的位置。当比较字符串'2'和字符串'100'时,它首先比较'2'和'1'。“2”在“1”之后出现(“2”的ASCII值大于“1”的ASCII值),因此,“100”将在“2”之前以字母排序的形式出现。这与比较“b”和“azz”完全相同——因为“a”在“b”之前,所以“azz”会在“b”之前排序。

Your text is pointing this out because this behavior while understandable is non-intuitive. You would expect a sort to place '100' after '2' since 2 < 100, but that is not was the sort does.

你的文章指出了这一点,因为这种行为虽然可以理解,但并不直观。你可能会期望一个排序将'100'放在'2'之后,因为2 < 100,但这不是排序所做的。

#1


5  

It means that numbers treated as strings are not sorted in numerical order but in lexical order, the same way words are sorted in a dictionary. That is, characters are compared one at a time from the left side.

这意味着,作为字符串处理的数字不是按照数字顺序排序的,而是按照词汇顺序排序,就像字典中对单词排序一样。也就是说,每个字符都是从左边来比较的。

In your first example, "2" is greater than "100" because the '2' is compared to the '1' and found to be bigger. Compare this to the ordering of "C" and "BAA" in a dictionary.

在第一个例子中,“2”大于“100”,因为“2”与“1”比较,发现它更大。与字典中“C”和“BAA”的顺序相比。

In your second example, "1" is less than "10" because the "1" fully matches the "1" in the left side of "10", but the "10" has characters following the match. Therefore it is greater. Again, compare this to the ordering of "B" and "BA" in a dictionary.

在第二个示例中,“1”小于“10”,因为“1”与“10”左边的“1”完全匹配,但“10”在匹配后具有字符。因此,更大。同样,将它与字典中“B”和“BA”的顺序进行比较。

#2


0  

You are exactly correct in assuming that they are sorted by ASCII values - this is called an alphabetic sort. The strings are sorted not as numeric values but as text.

您完全正确地假设它们是按ASCII值排序的——这被称为字母排序。字符串不是按数字值排序的,而是作为文本排序的。

The alphabetic sort compares the values position by position. When comparing the string '2' with the string '100' it starts by comparing '2' with '1'. '2' comes after '1' (the ASCII values of '2' is greater than the ASCII value of '1') alphabetically so the comparison stops so '100' will be listed before '2' in an alphabetic sort. This is exactly equivalent as comparing 'b' to 'azz' - since 'a' comes before 'b', 'azz' will be sorted before 'b'.

字母排序按位置比较值的位置。当比较字符串'2'和字符串'100'时,它首先比较'2'和'1'。“2”在“1”之后出现(“2”的ASCII值大于“1”的ASCII值),因此,“100”将在“2”之前以字母排序的形式出现。这与比较“b”和“azz”完全相同——因为“a”在“b”之前,所以“azz”会在“b”之前排序。

Your text is pointing this out because this behavior while understandable is non-intuitive. You would expect a sort to place '100' after '2' since 2 < 100, but that is not was the sort does.

你的文章指出了这一点,因为这种行为虽然可以理解,但并不直观。你可能会期望一个排序将'100'放在'2'之后,因为2 < 100,但这不是排序所做的。