It is not only about ASCII codes.
Yes, ASCII code of /
is 0x2F and ASCII code of -
is 0x2D, but string comparison rules depend on collation and these rules can be quite complex, taking into account not only values of the character code.
The -
symbol is treated in a special way in some collations.
Here is a full example:
DECLARE @T1 TABLE (Value varchar(100) COLLATE Chinese_PRC_CI_AS);
DECLARE @T2 TABLE (Value varchar(100) COLLATE Latin1_General_CI_AS);
DECLARE @T3 TABLE (Value varchar(100) COLLATE SQL_Latin1_General_CP1_CI_AS);
INSERT INTO @T1 VALUES
('abc'),
('abc-def'),
('abcdef'),
('abc-');
INSERT INTO @T2 VALUES
('abc'),
('abc-def'),
('abcdef'),
('abc-');
INSERT INTO @T3 VALUES
('abc'),
('abc-def'),
('abcdef'),
('abc-');
SELECT * FROM @T1 ORDER BY Value;
SELECT * FROM @T2 ORDER BY Value;
SELECT * FROM @T3 ORDER BY Value;
Result
T1 (Chinese_PRC_CI_AS)
+---------+
| Value |
+---------+
| abc |
| abc- |
| abcdef |
| abc-def |
+---------+
T2 (Latin1_General_CI_AS)
+---------+
| Value |
+---------+
| abc |
| abc- |
| abcdef |
| abc-def |
+---------+
T3 (SQL_Latin1_General_CP1_CI_AS)
+---------+
| Value |
+---------+
| abc |
| abc- |
| abc-def |
| abcdef |
+---------+
Note, that SQL collation in the third table produced result in a different order.
Note also, that if you change the column type to nvarchar
, the effect disappears in this specific example. In other words, rules for treating the -
symbol depend on collation and on the type of the value.