- 类别:
字符串和二进制函数 (通用)
REVERSE¶
颠倒字符串中字符的顺序或二进制值中字节的顺序。
返回值的长度与输入相同,但字符/字节顺序相反。如果 subject
为 NULL,则结果也为 NULL。
语法¶
REVERSE(<subject>)
排序规则详细信息¶
No impact.
The collation of the result is the same as the collation of the input.
In languages where the alphabet contains digraphs or trigraphs (such as "Dz" and "Dzs" in Hungarian), each character in each digraph and trigraph is treated as an independent character, not as part of a single multi-character letter.
例如,具有 2 个字符和 3 个字符字母的语言(例如匈牙利语中的“dzs”,捷克语中的“ch”)会根据单个字符而不是字母进行颠倒。有关示例,请参阅以下 示例 部分。
示例¶
以下示例颠倒了字符串:
SELECT REVERSE('Hello, world!'); +--------------------------+ | REVERSE('HELLO, WORLD!') | |--------------------------| | !dlrow ,olleH | +--------------------------+
以下示例颠倒了日期:
SELECT '2019-05-22'::DATE, REVERSE('2019-05-22'::DATE) AS reversed; +--------------------+------------+ | '2019-05-22'::DATE | REVERSED | |--------------------+------------| | 2019-05-22 | 22-50-9102 | +--------------------+------------+
以下示例显示,在单个字母由多个字符组成的语言中,REVERSE
根据字符(而不是字母)进行颠倒:
CREATE TABLE strings (s1 VARCHAR COLLATE 'en', s2 VARCHAR COLLATE 'hu'); INSERT INTO strings (s1, s2) VALUES ('dzsa', COLLATE('dzsa', 'hu'));SELECT s1, s2, REVERSE(s1), REVERSE(s2) FROM strings; +------+------+-------------+-------------+ | S1 | S2 | REVERSE(S1) | REVERSE(S2) | |------+------+-------------+-------------| | dzsa | dzsa | aszd | aszd | +------+------+-------------+-------------+