Oracle兼容-函数-REPLACE()函数


1. 语法

1. REPLACE(char, search_string [, replace_string])

2. 定义和用法

REPLACE() 函数的作用是根据给定的字符串匹配并替换原字符串,并返回替换后的字符串。

参数:

  • char:待替换字符串。
  • search_string:搜索字符串。
  • replace_string:替换后的字符串。本参数可选,当未指定时,则认为 replace_string 为空字符串。

当 char、search_string、replace_string 中包含转义字符(例如:\0、\'、''、\"、\\、\b、\B、\n、\N、\r、\R、\t、\T、\z、\Z)时,不会视为2个字符,而视为1个字符来处理。

3. Oracle兼容说明

因为GreatSQL已原生支持 REPLACE() 函数,因此想要在GreatSQL中使用扩展后的 REPLACE() 函数时,需要先执行 SET sql_mode = ORACLE; 激活Oracle兼容模式。

在原生默认模式(SET sql_mode = DEFAULT)下,参数 replace_string 必须指定,不能为空。

当 replace_string 值为 NULL 时,在 DEFAULT 和 ORACLE 两种不同模式下的返回结果也不同。

4. 示例

1. greatsql> SET sql_mode = ORACLE;
2. greatsql> SELECT REPLACE('GreatSQL tcp port 3306', '3306');
3. +-------------------------------------------+
4. | REPLACE('GreatSQL tcp port 3306', '3306') |
5. +-------------------------------------------+
6. | GreatSQL tcp port                         |
7. +-------------------------------------------+

9. greatsql> SELECT REPLACE('GreatSQL tcp port 3306 and 33060', '3306');
10. +-----------------------------------------------------+
11. | REPLACE('GreatSQL tcp port 3306 and 33060', '3306') |
12. +-----------------------------------------------------+
13. | GreatSQL tcp port  and 0                            |
14. +-----------------------------------------------------+

16. greatsql> SELECT REPLACE('GreatSQL tcp port 3306 and 33060', '3', '4');
17. +-------------------------------------------------------+
18. | REPLACE('GreatSQL tcp port 3306 and 33060', '3', '4') |
19. +-------------------------------------------------------+
20. | GreatSQL tcp port 4406 and 44060                      |
21. +-------------------------------------------------------+

23. greatsql> SELECT REPLACE('000012003', '0');
24. +---------------------------+
25. | REPLACE('000012003', '0') |
26. +---------------------------+
27. | 123                       |
28. +---------------------------+

30. greatsql> SELECT REPLACE('000012003', '0', ' ');
31. +--------------------------------+
32. | REPLACE('000012003', '0', ' ') |
33. +--------------------------------+
34. |     12  3                      |
35. +--------------------------------+

37. greatsql> SELECT REPLACE('GreatSQL', 'a', NULL);
38. +--------------------------------+
39. | REPLACE('GreatSQL', 'a', NULL) |
40. +--------------------------------+
41. | GretSQL                        |
42. +--------------------------------+

44. -- 最后一个案例,切换到DEFUALT模式下结果不同
45. greatsql> SET sql_mode = DEFAULT;
46. greatsql> SELECT REPLACE('GreatSQL', 'a', NULL);
47. +--------------------------------+
48. | REPLACE('GreatSQL', 'a', NULL) |
49. +--------------------------------+
50. | NULL                           |
51. +--------------------------------+

53. -- 在DEFAULT模式下,不能省略第三个参数
54. greatsql> SELECT REPLACE('GreatSQL', 'a');
55. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx