0

我在一个表中有三列

Column names with datatypes
First_week - Number 
Days       - Number
Second_week- Number

Column Values
8
6
11

我想连接这些值,使得结果返回 8/6/11 如果任何列值为空,则连接必须为 8/-/11 如果全部为空,则结果必须为 -/-/- 这怎么可能在oracle查询中实现?

4

3 回答 3

2

为此,您可以使用decode,nvl函数或case表达式。下面是一个使用示例decode

with your_table(First_week, Days, Second_week) as(
  select 8, 6, 11      from dual union all
  select 5, null, 12   from dual union all
  select null, 7, null from dual union all
  select null, null, null from dual
  )
select decode(to_char(First_week), null, '-', to_char(First_week)) || '/' ||
       decode(to_char(Days), null, '-', to_char(Days))             || '/' ||
       decode(to_char(Second_week), null, '-', to_char(Second_week)) as result
  from your_table


RESULT
---------
8/6/11
5/-/12
-/7/-
-/-/-
于 2013-01-22T04:05:49.663 回答
1

http://www.techonthenet.com/oracle/functions/coalesce.php

“在 Oracle/PLSQL 中,coalesce 函数返回列表中的第一个非 null 表达式。如果所有表达式的计算结果为 null,则 coalesce 函数将返回 null。”

http://docs.oracle.com/cd/B19306_01/server.102/b14200/operators003.htm#i997789

"|| 连接字符串和 CLOB 数据。"

现在我们有了所有的构建块来编写类似的东西:

COALESCE(First_week, '-') || '/' || 合并(天,'-')|| '/' || COALESCE(Second_week, '-')

于 2013-01-22T03:56:55.107 回答
1

那这个呢

SELECT    NVL (TO_CHAR (FIRST_WEEK), '-')
       || '/'
       || NVL (TO_CHAR (DAYS), '-')
       || '/'
       || NVL (TO_CHAR (SECOND_WEEK), '-')
  FROM YOUR_TABLE
于 2013-01-22T07:39:13.730 回答