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

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

    1. <legend id='c92tU'><style id='c92tU'><dir id='c92tU'><q id='c92tU'></q></dir></style></legend>

        日期的 Oracle SQL 比较返回错误的结果

        Oracle SQL comparison of DATEs returns wrong result(日期的 Oracle SQL 比较返回错误的结果)
          <bdo id='sp9tY'></bdo><ul id='sp9tY'></ul>
        • <tfoot id='sp9tY'></tfoot>

          1. <legend id='sp9tY'><style id='sp9tY'><dir id='sp9tY'><q id='sp9tY'></q></dir></style></legend>
              • <small id='sp9tY'></small><noframes id='sp9tY'>

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

                    <tbody id='sp9tY'></tbody>
                  本文介绍了日期的 Oracle SQL 比较返回错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在数据库 (DATETIME) 类型中有 REPORTDATE 列.我只想从 DATETIME 中提取 DATE 值,然后每天执行 COUNT 并放置 WHERE 子句以限制仅晚于某个特定日期.

                  I have REPORTDATE column in database (DATETIME) type. I want to extract only DATE value from the DATETIME, then to do COUNT for each day and to put WHERE clause to restrict only dates later than some specific date.

                  所以我有这个条款:

                  SELECT to_char(REPORTDATE, 'DD.MM.YYYY') AS MY, COUNT(*) from INCIDENT
                  where to_char(REPORTDATE, 'DD.MM.YYYY')>'09.11.2013'
                  
                  GROUP BY to_char(REPORTDATE, 'DD.MM.YYYY')
                  

                  它返回我的结果,但我可以注意到错误的结果,例如:30.10.2013 这是错误的结果.

                  It returns me results but but I can notice wrong result such as : 30.10.2013 which is wrong result.

                  如何解决这个问题?

                  推荐答案

                  WHERE to_char(REPORTDATE, 'DD.MM.YYYY')>'09.11.2013'

                  WHERE to_char(REPORTDATE, 'DD.MM.YYYY')>'09.11.2013'

                  您正在比较两个STRINGS.您需要比较 DATE.正如我在这里的另一个答案中已经说过的,您需要保留用于 DATE 计算的日期.TO_CHAR 用于显示,TO_DATE 用于将字符串文字转换为 DATE.

                  You are comparing two STRINGS. You need to compare the DATEs. As I already said in the other answer here, you need to leave the date as it is for DATE calculations. TO_CHAR is for display, and TO_DATE is to convert a string literal into DATE.

                  SELECT TO_CHAR(REPORTDATE, 'DD.MM.YYYY'),
                    COUNT(*)
                  FROM TABLE
                  WHERE REPORTDATE > TO_DATE('09.11.2013', 'DD.MM.YYYY')
                  GROUP BY TO_CHAR(REPORTDATE, 'DD.MM.YYYY') 
                  

                  此外,REPORTDATE 是一个 DATE 列,因此它将包含 datetime 元素.所以,如果你想在比较时排除时间元素,你需要使用TRUNC

                  Also, REPORTDATE is a DATE column, hence it will have datetime element. So, if you want to exclude the time element while comparing, you need to use TRUNC

                  WHERE TRUNC(REPORTDATE) > TO_DATE('09.11.2013', 'DD.MM.YYYY')
                  

                  但是,在日期上应用TRUNC会抑制该列上的任何常规索引.从性能的角度来看,最好使用日期范围条件.

                  However, applying TRUNC on the date column would suppress any regular index on that column. From performance point of view, better use a Date range condition.

                  例如

                  WHERE REPORTDATE
                  BETWEEN 
                          TO_DATE('09.11.2013', 'DD.MM.YYYY')
                  AND     
                          TO_DATE('09.11.2013', 'DD.MM.YYYY') +1
                  

                  这篇关于日期的 Oracle SQL 比较返回错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                    <bdo id='jLY6p'></bdo><ul id='jLY6p'></ul>

                        <tfoot id='jLY6p'></tfoot>

                          <tbody id='jLY6p'></tbody>
                      • <small id='jLY6p'></small><noframes id='jLY6p'>

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

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