<small id='fC1KA'></small><noframes id='fC1KA'>

    <legend id='fC1KA'><style id='fC1KA'><dir id='fC1KA'><q id='fC1KA'></q></dir></style></legend>

      <bdo id='fC1KA'></bdo><ul id='fC1KA'></ul>

      1. <tfoot id='fC1KA'></tfoot>
        <i id='fC1KA'><tr id='fC1KA'><dt id='fC1KA'><q id='fC1KA'><span id='fC1KA'><b id='fC1KA'><form id='fC1KA'><ins id='fC1KA'></ins><ul id='fC1KA'></ul><sub id='fC1KA'></sub></form><legend id='fC1KA'></legend><bdo id='fC1KA'><pre id='fC1KA'><center id='fC1KA'></center></pre></bdo></b><th id='fC1KA'></th></span></q></dt></tr></i><div id='fC1KA'><tfoot id='fC1KA'></tfoot><dl id='fC1KA'><fieldset id='fC1KA'></fieldset></dl></div>

        如何处理 SELECT 语句中的 to_date 异常以忽略这些行?

        时间:2023-09-18

          <tbody id='FsSHn'></tbody>

          <bdo id='FsSHn'></bdo><ul id='FsSHn'></ul>
          <i id='FsSHn'><tr id='FsSHn'><dt id='FsSHn'><q id='FsSHn'><span id='FsSHn'><b id='FsSHn'><form id='FsSHn'><ins id='FsSHn'></ins><ul id='FsSHn'></ul><sub id='FsSHn'></sub></form><legend id='FsSHn'></legend><bdo id='FsSHn'><pre id='FsSHn'><center id='FsSHn'></center></pre></bdo></b><th id='FsSHn'></th></span></q></dt></tr></i><div id='FsSHn'><tfoot id='FsSHn'></tfoot><dl id='FsSHn'><fieldset id='FsSHn'></fieldset></dl></div>

            <small id='FsSHn'></small><noframes id='FsSHn'>

              <legend id='FsSHn'><style id='FsSHn'><dir id='FsSHn'><q id='FsSHn'></q></dir></style></legend>

                <tfoot id='FsSHn'></tfoot>
                • 本文介绍了如何处理 SELECT 语句中的 to_date 异常以忽略这些行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下查询,我试图将其用作我正在处理的水晶报表中的命令.

                  I have the following query that I am attempting to use as a COMMAND in a crystal report that I am working on.

                  SELECT * FROM myTable
                  WHERE to_date(myTable.sdate, 'MM/dd/yyyy') <= {?EndDate}
                  

                  这很好用,但我唯一担心的是日期可能不总是正确的格式(由于用户错误).我知道当 to_date 函数失败时它会抛出一个异常..是否有可能以忽略我的 SELECT 语句中的相应行的方式处理这个异常?因为否则如果整个数据库中只有一个日期格式不正确,我的报告就会中断.

                  This works fine, however my only concern is that the date may not always be in the correct format (due to user error). I know that when the to_date function fails it throws an exception.. is it possible to handle this exception in such a way that it ignores the corresponding row in my SELECT statement? Because otherwise my report would break if only one date in the entire database is incorrectly formatted.

                  我查看了 Oracle 是否提供了 isDate 函数,但您似乎应该只处理异常.任何帮助将不胜感激.谢谢!!

                  I looked to see if Oracle offers an isDate function, but it seems like you are supposed to just handle the exception. Any help would be greatly appreciated. Thanks!!

                  推荐答案

                  响应 Tony 的评论,您最好将日期存储在 DATE 列中,而不是强制前端查询工具查找和处理这些异常.

                  Echoing Tony's comment, you'd be far better off storing dates in DATE columns rather than forcing a front-end query tool to find and handle these exceptions.

                  但是,如果您遇到不正确的数据模型,那么在早期版本中最简单的选择是创建一个函数来进行转换并处理错误,

                  If you're stuck with an incorrect data model, however, the simplest option in earlier versions is to create a function that does the conversion and handles the error,

                  CREATE OR REPLACE FUNCTION my_to_date( p_date_str IN VARCHAR2,
                                                p_format_mask IN VARCHAR2 )
                    RETURN DATE
                  IS
                    l_date DATE;
                  BEGIN
                    l_date := to_date( p_date_str, p_format_mask );
                    RETURN l_date;
                  EXCEPTION
                    WHEN others THEN
                      RETURN null;
                  END my_to_date;
                  

                  您的查询将变为

                  SELECT * 
                    FROM myTable
                   WHERE my_to_date(myTable.sdate, 'MM/dd/yyyy') <= {?EndDate}
                  

                  当然,您很可能希望在 MY_TO_DATE 调用上有一个基于函数的索引,以便使这个查询相当有效.

                  Of course, you'd most likely want a function-based index on the MY_TO_DATE call in order to make this query reasonably efficient.

                  在 12.2 中,Oracle 添加了对 to_datecast 函数的扩展来处理该错误的转换

                  In 12.2, Oracle has added extensions to the to_date and cast functions to handle conversions that error

                  SELECT * 
                    FROM myTable
                   WHERE to_date(myTable.sdate default null on conversion error, 'MM/dd/yyyy') <= {?EndDate}
                  

                  如果您要查找所有是(或不是)有效日期的行,您也可以使用 validate_conversion 函数.

                  You could also use the validate_conversion function if you're looking for all the rows that are (or are not) valid dates.

                  SELECT *
                    FROM myTable 
                   WHERE validate_conversion( myTable.sdate as date, 'MM/DD/YYYY' ) = 1 
                  

                  这篇关于如何处理 SELECT 语句中的 to_date 异常以忽略这些行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 Oracle 数据库创建 Excel 电子表格 下一篇:Oracle 11G 中的 INSERT SELECT 语句

                  相关文章

                • <i id='TIyjp'><tr id='TIyjp'><dt id='TIyjp'><q id='TIyjp'><span id='TIyjp'><b id='TIyjp'><form id='TIyjp'><ins id='TIyjp'></ins><ul id='TIyjp'></ul><sub id='TIyjp'></sub></form><legend id='TIyjp'></legend><bdo id='TIyjp'><pre id='TIyjp'><center id='TIyjp'></center></pre></bdo></b><th id='TIyjp'></th></span></q></dt></tr></i><div id='TIyjp'><tfoot id='TIyjp'></tfoot><dl id='TIyjp'><fieldset id='TIyjp'></fieldset></dl></div>

                    1. <tfoot id='TIyjp'></tfoot>

                      <small id='TIyjp'></small><noframes id='TIyjp'>

                      <legend id='TIyjp'><style id='TIyjp'><dir id='TIyjp'><q id='TIyjp'></q></dir></style></legend>
                        <bdo id='TIyjp'></bdo><ul id='TIyjp'></ul>