Oracle中DateField的日期

时间:2016-06-24 12:19:48

标签: oracle extract

我正在尝试将我的发货日期分解为各种组件,以便我可以按照我需要的方式查询。我在Oracle查询中使用以下代码:

EXTRACT (DAY FROM "AR_TRX_HEADER"."SHIPDATETIME") AS DofM,
EXTRACT (YEAR FROM(ADD_MONTHS ("AR_TRX_HEADER"."SHIPDATETIME", 1))) AS FY,

我把YEAR拉得很好,但是每次尝试拉扯DAY时我都会收到错误。它告诉我,我错过了一个正确的括号。有什么想法吗?

任何帮助将不胜感激。谢谢!

这是整个查询(你会在这个查询中看到我一直把DayOfMonth作为一个字符字段 - 我现在正在重写它,试图把它作为一个数字。我只是修改了提取月来做那个它工作正常。我将在下一个财政季度工作。):

SELECT
         "AR_PRODUCT_VIEW"."CUSTID" || ' - ' || "AR_PRODUCT_VIEW"."CUSTNAME" AS Customer,
         "AR_PRODUCT_VIEW"."PRODUCTID" || ' - ' || "AR_PRODUCT_VIEW"."PRODUCTDESCR" AS Product,
         SUM("AR_PRODUCT_VIEW"."QNT1") AS Gallons,
         TO_CHAR("AR_TRX_HEADER"."SHIPDATETIME",'MON') AS MonthName,
         TO_CHAR("AR_TRX_HEADER"."SHIPDATETIME",'DD')  AS DayOfMonth,
         EXTRACT (YEAR FROM(ADD_MONTHS ("AR_TRX_HEADER"."SHIPDATETIME", 1))) AS FY,
         EXTRACT (MONTH FROM(ADD_MONTHS ("AR_TRX_HEADER"."SHIPDATETIME", 1))) AS FM,
         TO_CHAR(ADD_MONTHS ("AR_TRX_HEADER"."SHIPDATETIME",1),'Q') AS FQ,
floor(
  (
  trunc("AR_TRX_HEADER"."SHIPDATETIME") - 
  case 
  -- if December is current month, than use 1st of this month
  when to_char("AR_TRX_HEADER"."SHIPDATETIME", 'MM') = 12 then trunc("AR_TRX_HEADER"."SHIPDATETIME", 'MM')
  -- else, use 1st December of previous year
  else add_months(trunc("AR_TRX_HEADER"."SHIPDATETIME", 'YYYY'), -1)
  end
  ) / 7
) + 1
AS FW,
         "FC_NAME"."LASTCONAME" AS SalesPerson ,
         "AR_PRODUCT_VIEW"."SONO" AS InvoiceNumber,
         TO_CHAR("AR_TRX_HEADER"."SHIPDATETIME",'mm/dd/yyyy') AS InvoiceDate,
         "AR_PRODUCT_VIEW"."COMMODITY" AS ProductCategory
    FROM "AR_PRODUCT_VIEW",   
         "AR_TRX_LINE",   
         "AR_TRX_HEADER",   
         "AR_CUST",   
         "FC_NAME"  
   WHERE ( "AR_TRX_LINE"."ARTRXHDRIDX" = "AR_TRX_HEADER"."ARTRXHDRIDX" ) and  
         ( "AR_PRODUCT_VIEW"."ARTRXLINESEQ" = "AR_TRX_LINE"."ARTRXLINESEQ" ) and  
         ( "AR_PRODUCT_VIEW"."ARTRXHDRIDX" = "AR_TRX_LINE"."ARTRXHDRIDX" ) and  
         ( "AR_PRODUCT_VIEW"."ARTRXLINETRXTYPE" = "AR_TRX_LINE"."ARTRXLINETRXTYPE" ) and  
         ( "AR_PRODUCT_VIEW"."CUSTNAMEIDX" = "AR_CUST"."CUSTNAMEIDX" ) and  
         ( "AR_CUST"."SALESPERSONNAMEIDX" = "FC_NAME"."NAMEIDX" ) 
GROUP BY "AR_PRODUCT_VIEW"."PRODUCTDESCR",   
         "AR_PRODUCT_VIEW"."CUSTNAME",   
         "AR_PRODUCT_VIEW"."PRODUCTID",   
         "AR_PRODUCT_VIEW"."CUSTID",   
         "AR_PRODUCT_VIEW"."COMMODITY",   
         "AR_PRODUCT_VIEW"."PSSIZE",   
         "AR_PRODUCT_VIEW"."UOM",   
         "AR_PRODUCT_VIEW"."QNTTITLE",   
         "AR_PRODUCT_VIEW"."AMTTITLE",   
         "AR_PRODUCT_VIEW"."SONO",   
         "AR_TRX_HEADER"."SHIPDATETIME",   
         "AR_TRX_HEADER"."SODATETIME",   
         "AR_PRODUCT_VIEW"."SALESPERSON",   
         "FC_NAME"."LASTCONAME" 
ORDER BY "AR_TRX_HEADER"."SHIPDATETIME" ASC,   
         "AR_PRODUCT_VIEW"."SONO" ASC   

2 个答案:

答案 0 :(得分:0)

使用以下查询测试chk

 select EXTRACT (DAY FROM to_date('24-june-2016')) AS DOFM ,TO_CHAR(to_date('24-june-2016'), 'Day') from dual;

然后尝试使用您的查询

select EXTRACT (DAY FROM "AR_TRX_HEADER"."SHIPDATETIME") AS DofM from your_table;

注意: - “AR_TRX_HEADER”。“SHIPDATETIME”应为日期类型,否则您需要转换为日期类型。

答案 1 :(得分:0)

我能够让这个工作。它可能不是最有效的方法来完成它,但它确实有效,它给了我我需要的东西。谢谢你的输入。开始向他人解释一些内容总是有帮助的。它让你自己更多地思考。

  SELECT
         "AR_PRODUCT_VIEW"."CUSTID" || ' - ' || "AR_PRODUCT_VIEW"."CUSTNAME" AS Customer,
         "AR_PRODUCT_VIEW"."PRODUCTID" || ' - ' || "AR_PRODUCT_VIEW"."PRODUCTDESCR" AS Product,
         SUM("AR_PRODUCT_VIEW"."QNT1") AS Gallons,
         TO_CHAR("AR_TRX_HEADER"."SHIPDATETIME",'MON') AS MonthName,
         TO_NUMBER(TO_CHAR("AR_TRX_HEADER"."SHIPDATETIME",'DD'))  AS DayOfMonth,
         EXTRACT (YEAR FROM(ADD_MONTHS ("AR_TRX_HEADER"."SHIPDATETIME", 1))) AS FY,
         EXTRACT (MONTH FROM(ADD_MONTHS ("AR_TRX_HEADER"."SHIPDATETIME", 1))) AS FM,
         TO_NUMBER(TO_CHAR(ADD_MONTHS ("AR_TRX_HEADER"."SHIPDATETIME",1),'Q')) AS FQ,
floor(
  (
  trunc("AR_TRX_HEADER"."SHIPDATETIME") - 
  case 
  -- if December is current month, than use 1st of this month
  when to_char("AR_TRX_HEADER"."SHIPDATETIME", 'MM') = 12 then trunc("AR_TRX_HEADER"."SHIPDATETIME", 'MM')
  -- else, use 1st December of previous year
  else add_months(trunc("AR_TRX_HEADER"."SHIPDATETIME", 'YYYY'), -1)
  end
  ) / 7
) + 1
AS FW,
         "FC_NAME"."LASTCONAME" AS SalesPerson ,
         TO_NUMBER("AR_PRODUCT_VIEW"."SONO") AS InvoiceNumber,
         TO_CHAR("AR_TRX_HEADER"."SHIPDATETIME",'mm/dd/yyyy') AS InvoiceDate,
         "AR_PRODUCT_VIEW"."COMMODITY" AS ProductCategory
    FROM "AR_PRODUCT_VIEW",   
         "AR_TRX_LINE",   
         "AR_TRX_HEADER",   
         "AR_CUST",   
         "FC_NAME"  
   WHERE ( "AR_TRX_LINE"."ARTRXHDRIDX" = "AR_TRX_HEADER"."ARTRXHDRIDX" ) and  
         ( "AR_PRODUCT_VIEW"."ARTRXLINESEQ" = "AR_TRX_LINE"."ARTRXLINESEQ" ) and  
         ( "AR_PRODUCT_VIEW"."ARTRXHDRIDX" = "AR_TRX_LINE"."ARTRXHDRIDX" ) and  
         ( "AR_PRODUCT_VIEW"."ARTRXLINETRXTYPE" = "AR_TRX_LINE"."ARTRXLINETRXTYPE" ) and  
         ( "AR_PRODUCT_VIEW"."CUSTNAMEIDX" = "AR_CUST"."CUSTNAMEIDX" ) and  
         ( "AR_CUST"."SALESPERSONNAMEIDX" = "FC_NAME"."NAMEIDX" ) 
GROUP BY "AR_PRODUCT_VIEW"."PRODUCTDESCR",   
         "AR_PRODUCT_VIEW"."CUSTNAME",   
         "AR_PRODUCT_VIEW"."PRODUCTID",   
         "AR_PRODUCT_VIEW"."CUSTID",   
         "AR_PRODUCT_VIEW"."COMMODITY",   
         "AR_PRODUCT_VIEW"."PSSIZE",   
         "AR_PRODUCT_VIEW"."UOM",   
         "AR_PRODUCT_VIEW"."QNTTITLE",   
         "AR_PRODUCT_VIEW"."AMTTITLE",   
         "AR_PRODUCT_VIEW"."SONO",   
         "AR_TRX_HEADER"."SHIPDATETIME",   
         "AR_TRX_HEADER"."SODATETIME",   
         "AR_PRODUCT_VIEW"."SALESPERSON",   
         "FC_NAME"."LASTCONAME" 
ORDER BY "AR_TRX_HEADER"."SHIPDATETIME" ASC,   
         "AR_PRODUCT_VIEW"."SONO" ASC