Skip to content

Commit 07fc605

Browse files
💬Generate LLM translations (#2299)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 6a63004 commit 07fc605

File tree

1 file changed

+56
-0
lines changed
  • docs/cn/sql-reference/20-sql-functions/03-conditional-functions

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: DECODE
3+
---
4+
5+
DECODE 函数按顺序将选择表达式与每个搜索表达式进行比较。一旦搜索表达式与选择表达式匹配,就返回相应的结果表达式。如果没有找到匹配项且提供了默认值,则返回默认值。
6+
7+
## 语法
8+
9+
```sql
10+
DECODE( <expr>, <search1>, <result1> [, <search2>, <result2> ... ] [, <default> ] )
11+
```
12+
13+
## 参数
14+
15+
- `expr`: 与每个搜索表达式进行比较的"选择表达式"。通常是一个列,但也可以是子查询、字面量或其他表达式。
16+
- `searchN`: 与选择表达式进行比较的搜索表达式。如果找到匹配项,则返回相应的结果。
17+
- `resultN`: 如果相应的搜索表达式与选择表达式匹配,将返回的值。
18+
- `default`: 可选。如果提供且没有搜索表达式匹配,则返回此默认值。
19+
20+
## 使用说明
21+
22+
-`CASE` 不同,选择表达式中的 NULL 值与搜索表达式中的 NULL 值匹配。
23+
- 如果多个搜索表达式匹配,只返回第一个匹配项的结果。
24+
25+
## 示例
26+
27+
```sql
28+
CREATE TABLE t (a VARCHAR);
29+
INSERT INTO t (a) VALUES
30+
('1'),
31+
('2'),
32+
(NULL),
33+
('4');
34+
```
35+
36+
带有默认值 'other' 的示例 (注意 NULL 等于 NULL):
37+
38+
```sql
39+
SELECT a, decode(a,
40+
1, 'one',
41+
2, 'two',
42+
NULL, '-NULL-',
43+
'other'
44+
) AS decode_result
45+
FROM t;
46+
```
47+
48+
结果:
49+
```
50+
┌─a─┬─decode_result─┐
51+
│ 1 │ one │
52+
│ 2 │ two │
53+
│ │ -NULL- │
54+
│ 4 │ other │
55+
└───┴───────────────┘
56+
```

0 commit comments

Comments
 (0)