日期:2014-05-17  浏览次数:21126 次

oracle 查询两个日期并拼接起来
我oracle数据库中有两个时间time1与time2,现在要显示到页面的的形式为:   日期:time1 到 time2 

我目前用了两个时间都不为空的写法:
select time1||'到'||time2 from 表名

但是这两个时间都有可能为空
如果其中一个为空则显示不为空的一个日期,例如time1为空时则展示为: 日期:time2
如果两个都为空则返回空
我想问问如果直接在查数据的时候就把这个拼接好SQL应该怎么写,使用函数或者方法会更好点吗?麻烦给出写法

------解决方案--------------------
试一试这样可以不
select case when time1 is not null and time2 is not null then time1
------解决方案--------------------
'到'
------解决方案--------------------
time2
                      when time1 is null and time2 is not null then '日期:'
------解决方案--------------------
time2
                      when time2 is null and time1 is not null then '日期:'
------解决方案--------------------
time1
                       else null end
from 表名
------解决方案--------------------
引用:
Quote: 引用:

试一试这样可以不
select case when time1 is not null and time2 is not null then time1
------解决方案--------------------
'到'
------解决方案--------------------
time2
                      when time1 is null and time2 is not null then '日期:'
------解决方案--------------------
time2
                      when time2 is null and time1 is not null then '日期:'
------解决方案--------------------
time1
                       else null end
from 表名


恩,这个我有用过,就是没有想,简单的逻辑处理。3楼的逻辑看懂了,这算是方法还是存储过程?SQL写得少,比较菜

sql语句,因为在回答问题的时候我的建立临时表来获取一些数据,进行测试保证给你发的sql是准确的。使用了with as语句。
你测试时候在测试窗口将我发的所有代码复制执行就能看出来效果了。
你在看这种语句是,只关注with as后面跟的sql就行了。。具体with as用法,你可以上网查一下。。