按日期排序不使用小时和分钟来订购

时间:2014-01-08 13:25:43

标签: sql date oracle10g

我有一个使用函数的select查询。

该函数使子字符串成为字符串,获取日期,然后按日期排序,并以正确的顺序返回字符串。

所以我有我的SELECT:

 SELECT DISTINCT ec.id_echange_candidat,
 ( select f_tridate_concat( f_concat(iw.nom_declencheur ||' ' || iw.prenom_declencheur ||'->'||iw.nom_valideur ||' ' || iw.prenom_valideur || ' demandé le '||to_date(to_char(iw.date_creation), 'DD/MM/YY HH24:MI:SS')|| ' : ' ||iw.avis), '$', 'demandé le ', ' : ', '') 
    from t_instance_workflow iw 
    where ec.id_echange_candidat = iw.id_objet ) as resume_workflow
FROM t_echange_candidat ec  

功能:

create or replace
FUNCTION F_TRIDATE_CONCAT (p_chaine VARCHAR2, p_sepFonction VARCHAR2, p_sepAvChampTri VARCHAR2, p_sepApChampTri VARCHAR2, p_sepFinal VARCHAR2)
RETURN VARCHAR2 AS 
     v_str VARCHAR2(32767);

BEGIN
  SELECT replace(f_concat(ma_chaine), '$',p_sepFinal)   INTO v_str FROM
  (
  SELECT REGEXP_SUBSTR(p_chaine, '[^'||p_sepFonction||']+', 1, LEVEL) ma_chaine
  FROM dual
  CONNECT BY INSTR(p_chaine, p_sepFonction, 1, LEVEL - 1) > 0
  ORDER BY to_date(SUBSTR(ma_chaine,instr(ma_chaine,p_sepAvChampTri,1)+length(p_sepAvChampTri),(instr(ma_chaine,p_sepApChampTri,1))-(instr(ma_chaine,p_sepAvChampTri,1)+length(p_sepAvChampTri))),'DD/MM/YY HH24:MI:SS')
  )
  ;
  RETURN v_str;
END F_TRIDATE_CONCAT;

事情是,它似乎没有使用小时和分钟来制定它的顺序,所以如果我有这样的行:

line 1 text ... 10/12/2013 text ... (with 11 pm in db)
line 2 text ... 10/12/2013 text ... (with 10 pm in db)

我将获得此字符串:

line 1 text ... 10/12/2013 text ... 
line 2 text ... 10/12/2013 text ...

虽然我应该得到这个:

line 2 text ... 10/12/2013 text ... 
line 1 text ... 10/12/2013 text ...

因为lines 2 date已完整10/12/2013 10:00:00lines 1 date已完整10/12/2013 11:00:00

为什么不使用小时和分钟,而在我的功能中,我使用的格式日期如'DD / MM / YY HH24:MI:SS'在我的订单中?

编辑:在我的SELECT查询中使用to_date时出现“格式代码出现两次”错误消息...

这意味着它变成了:

 SELECT DISTINCT ec.id_echange_candidat,
 ( select f_tridate_concat( f_concat(iw.nom_declencheur ||' ' || iw.prenom_declencheur ||'->'||iw.nom_valideur ||' ' || iw.prenom_valideur || ' demandé le '||iw.date_creation|| ' : ' ||iw.avis), '$', 'demandé le ', ' : ', '') 
    from t_instance_workflow iw 
    where ec.id_echange_candidat = iw.id_objet ) as resume_workflow
FROM t_echange_candidat ec  


create or replace
FUNCTION f_concat (p_input VARCHAR2)
RETURN VARCHAR2
PARALLEL_ENABLE AGGREGATE USING t_f_concat;

create or replace
TYPE "T_F_CONCAT"                                                                                                                                                                                                                                      AS OBJECT ( "G_STRING" 
    VARCHAR2(32767),
    STATIC FUNCTION ODCIAggregateInitialize(sctx  IN OUT  t_f_concat)
    RETURN NUMBER ,

    MEMBER FUNCTION ODCIAggregateIterate(self   IN OUT  t_f_concat,
                                       value  IN      VARCHAR2 )
    RETURN NUMBER,
    MEMBER FUNCTION ODCIAggregateTerminate(self         IN   t_f_concat,
                                         returnValue  OUT  VARCHAR2,
                                         flags        IN   NUMBER)
    RETURN NUMBER,


  MEMBER FUNCTION ODCIAggregateMerge(self  IN OUT  t_f_concat,
                                     ctx2  IN      t_f_concat)
    RETURN NUMBER
  );

create or replace
type body t_f_concat is
    STATIC FUNCTION ODCIAggregateInitialize(sctx  IN OUT  t_f_concat)
    RETURN NUMBER IS
  BEGIN
    sctx := t_f_concat(NULL);
    RETURN ODCIConst.Success;
  END;

  MEMBER FUNCTION ODCIAggregateIterate(self   IN OUT  t_f_concat,
                                       value  IN      VARCHAR2 )
    RETURN NUMBER IS
  BEGIN
    SELF.g_string := self.g_string || '$' || value;
    RETURN ODCIConst.Success;
  END;

  MEMBER FUNCTION ODCIAggregateTerminate(self         IN   t_f_concat,
                                         returnValue  OUT  VARCHAR2,
                                         flags        IN   NUMBER)
    RETURN NUMBER IS
  BEGIN
    returnValue := RTRIM(LTRIM(SELF.g_string, '$'), '$');
    RETURN ODCIConst.Success;
  END;

  MEMBER FUNCTION ODCIAggregateMerge(self  IN OUT  t_f_concat,
                                     ctx2  IN      t_f_concat)
    RETURN NUMBER IS
  BEGIN
    SELF.g_string := SELF.g_string || '$' || ctx2.g_string;
    RETURN ODCIConst.Success;
  END;
  end;

1 个答案:

答案 0 :(得分:0)

试试这个:

WITH STRINGS
    AS (SELECT
             'Line 2' TEXT,
             '10/12/2013 10:00:00' STRING1
        FROM
             DUAL
        UNION ALL
        SELECT
             'Line 1' TEXT,
             '10/12/2013 11:00:00' STRING1
        FROM
             DUAL)
SELECT
      TEXT,
      TO_DATE ( STRING1,
              'DD/MM/YY HH24:MI:SS' )
FROM
      STRINGS
ORDER BY
      TO_DATE ( STRING1,
              'DD/MM/YY HH24:MI:SS' );

<强>输出

Line 2  12/10/2013 10:00:00 AM
Line 1  12/10/2013 11:00:00 AM

PS:您的转化中可能会拼错MI和MM,这会引发更新后的错误。如果您仍然遇到问题,请为您获得的功能和输出提供样本输入。还解释了在此函数中调用的f_concat。

关注:

这是错误的,这是您在选择

中的内容
SELECT
      TO_DATE ( TO_CHAR ( SYSDATE ),
              'DD/MM/YY HH24:MI:SS' )
FROM
      DUAL;

<强>输出:

1/8/2014

将其替换为

SELECT
      TO_DATE ( TO_CHAR ( SYSDATE,
                      'DD/MM/YY HH24:MI:SS' ),
              'DD/MM/YY HH24:MI:SS' )
FROM
      DUAL;

<强>输出:

1/8/2014 4:36:13 PM