表T中,有A,B,C,D四个字段,用于存放时间,数据设计时,是VARCHAR2类型
如:
  A               B         C          D
2008-07-29  2008-03-11  2007-05-25  2008-01-24请问,如何写一条SQL语句,能得到 A,B,C,D 时间值中最小的值。
如上例中,即是 2007-05-25 。

解决方案 »

  1.   

    select min(a)
    from(
    select to_date(A,'yyyy-mm-dd') a
    union all
    select to_date(B,'yyyy-mm-dd') a
    union all
    select to_date(C,'yyyy-mm-dd') a
    union all
    select to_date(D,'yyyy-mm-dd') a
    )
      

  2.   

    4个字段还好判断,只有1行数据
    select min(aa) from (
    select to_date(a,'yyyy-mm-dd') as aa from table union 
    select to_date(b,'yyyy-mm-dd') from table union 
    select to_date(c,'yyyy-mm-dd') from table union 
    select to_date(d,'yyyy-mm-dd') from table 
    )
      

  3.   

    select LEAST(to_date(a,'yyyy-mm-dd'),to_date(b,'yyyy-mm-dd'),to_date(c,'yyyy-mm-dd'),to_date(d,'yyyy-mm-dd')) from tablename ;
      

  4.   

    select min(A) from
    (
      (select A from T)
      union all
      (select B from T)
      union all
      (select C from T)
      union all
      (select D from T)  
    )
    ;
      

  5.   

    多谢楼上的各位高人,TO:TodayZ,你那样写是不是错了啊
    另外。 
    如果要写 WHERE语句C ,select A from T  where C 
    是不是要写成
    select min(A) from 

      (select A from T where C) 
      union all 
      (select B from T  where C) 
      union all 
      (select C from T where C) 
      union all 
      (select D from T where C)   

    ;???
    这样写好繁琐啊,有没有更简便的写法??
      

  6.   

    select LEAST(to_date(a,'yyyy-mm-dd'),to_date(b,'yyyy-mm-dd'),to_date(c,'yyyy-mm-dd'),to_date(d,'yyyy-mm-dd')) from tablename ;
      

  7.   

    LEAST函数是返回所有输入值的最小值。
    如果表中就一条记录,我的写法没有问题。
    select LEAST(a,b,c,d) from tablename ;
    如果表中有多条记录,并且你要取所有记录的最小值,那就在最外面加上一个min()函数。
    select min(LEAST(a,b,c,d)) from tablename ;btw: guoxiaoshou2000说的对,'yyyy-mm-dd'格式下,不用to_date效果是一样。