English 中文(简体)
下令在甲骨文举行VARCHAR2约会
原标题:Ordering VARCHAR2 dates in Oracle
  • 时间:2012-05-24 20:54:14
  •  标签:
  • sql
  • oracle

我在 VIEW 中有一个栏目,其中含有MM/DD/YYYY格式的日期值。

如果该列仅包含我所列格式中的日期值,我可以在有 < code> TO_DATE < /code > 的列上用 < code> by 进行 < code>,以使VIEW在该日期之前进行排序:

ORDER BY TO_DATE(MY_DATE_COLUMN,  MM/DD/YYYY ) desc

然而,本栏中的数据没有以MM/DD/YYYY格式格式格式。

无论如何,是否仅仅使用 SQL 才能将此列的排序为 DATE 列?

我认为这是不可能的,因为数据没有以 MM/DD/YYYY 格式格式格式格式,但我并不完全确定...

我不想在VIEW 中再增加一栏来完成这一点。

最佳回答

您可以在此列上创建一个函数, 试图转换日期, 但可以捕捉任何例外, 并以此排序 。

您也可以在默认日期为您选择的日期之前嵌入例外,并尝试多种转换格式。例如。

CREATE OR REPLACE FUNCTION my2date( p_str in VARCHAR2) RETURN DATE is

BEGIN
      RETURN to_date( p_str,  MM/DD/YYYY  );
EXCEPTION WHEN OTHERS THEN
    BEGIN
        RETURN to_date( p_str,  DD/MM/YYYY );
    EXCEPTION WHEN OTHERS THEN
        RETURN sysdate; /* or some other fixed date - depends on if you want these first or last */
    END;
END;
/

那就用这个条款:

ORDER BY my2date(MY_DATE_COLUMN) desc;
问题回答

一种方式是手动验证日期:

order by (case when substr(MY_DATE_COLUMN, 1, 2) between  01  and  12  and
                    substr(my_date_column, 3, 1) =  /  and
                    substr(my_date_column, 4, 2) between  01  and  31  and
                    substr(my_date_column, 5, 1) =  /  and
                    substr(my_date_column, 6, 4) between  1900  and  2100  and
                    len(my_date_column) = 10
               then to_date(MY_DATE_COLUMN,  mm/dd/yyyy )
          end) desc

这对多数目的来说可能足够好,尽管它允许2月30日。

您真正想要的是甲骨文中的 IsDate 函数, 但是它不存在 。

在张贴了这个之后,我意识到 可能有一个更容易的方法:

order by substr(my_date_column, 6, 4) desc, substr(MY_DATE_COLUMN, 1, 2) desc,
         substr(my_date_column, 4, 2) desc

它只是逐年、逐月、逐日、逐年地下降。 非日期会混杂在一起,取决于其中的内容。 然而,提问者似乎并不关心命令,而只是消除语法错误。

how about fix the data, and add a trigger to prevent further bad data... or add a proper date column instead of the varchar...

实现此目标的最简单方式可能是定义您自己的 to_date () 函数, 该函数返回无效( 或其他任何默认值, 如 sysdate ) 。 这可能是可能的, 因为 to_ date 只会在无法处理输入值时丢出一个例外 。

CREATE OR REPLACE FUNCTION to_date_null(date_str IN varchar2, format_mask IN varchar2)
RETURN DATE
IS
  l_date DATE;
BEGIN
  l_date := to_date( date_str, format_mask );
  RETURN l_date;
EXCEPTION
  WHEN others THEN
    RETURN null;
END to_date_null;

然后您可以在 order 中通过 条款在 命令中使用此函数

order by to_date_null(MY_DATE_COLUMN,  MM/DD/YYYY ) desc NULLS LAST;

通过指定 NULLLS First NULLLS LAST ,您可以定义非日期值的位置。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...