在TO_DATE函数中使用变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在TO_DATE函数中使用变量相关的知识,希望对你有一定的参考价值。

我正在尝试使用where子句中的今天日期通过LINKED SERVER从ORACLE返回数据

码:

DECLARE @var_date char(10)
SET @var_date = (SELECT CONVERT(DATE,GETDATE()))

SELECT TOP(25) * FROM OPENQUERY (OPICS, 'SELECT 
CASE 
  WHEN OPICS.FXDH.BR = 10 THEN ''Botswana''
  WHEN OPICS.FXDH.BR = 20 THEN ''Mozanmbique''
  WHEN OPICS.FXDH.BR = 30 THEN ''Rwanada''
  WHEN OPICS.FXDH.BR = 40 THEN ''Tanzania''
  WHEN OPICS.FXDH.BR = 50 THEN ''Zambia''
  WHEN OPICS.FXDH.BR = 60 THEN ''Zimbabwe''
END AS COUNTRY,
OPICS.FXDH.DEALNO,
to_char (OPICS.FXDH.DEALDATE,''YYYY-MM-DD'') as Deal_Date,
to_char (OPICS.FXDH.VDATE, ''YYYY-MM-DD'') as V_Date,
OPICS.FXDH.PS, 
OPICS.FXDH.CCY, 
OPICS.FXDH.CCYAMT, 
OPICS.FXDH.CCYRATE_8,
OPICS.FXDH.CTRCCY, 
to_char (OPICS.OPER.OPERNAME) as OPERNAME,
to_char (OPICS.CUST.CFN1) as CFN1,
to_char (OPICS.PROD.PDESC) as PDESC,
to_char (OPICS.PORT.PORTDESC) as Report_Description
FROM OPICS.FXDH FXDH
INNER JOIN OPICS.OPER OPER ON (FXDH.BR = OPER.BR) AND (FXDH.TRAD = OPER.OPER)
INNER JOIN OPICS.CUST CUST ON (FXDH.CUST = CUST.CNO)
INNER JOIN OPICS.PROD PROD ON (FXDH.PRODCODE = PROD.PCODE)
INNER JOIN OPICS.PORT PORT ON (FXDH.PORT = PORT.PORTFOLIO) AND (FXDH.BR = PORT.BR)
 WHERE OPICS.FXDH.DEALDATE = TO_DATE(''2018-08-29'', ''YYYY-MM-DD'')  
    --dateString
AND OPICS.FXDH.BR = 10
--ORDER BY OPICS.FXDH.DEALDATE DESC');

上面的代码工作正常并返回值

有没有办法可以在我需要/想要的日期使用我的变量@var_date而不是硬拷贝。

我尝试使用以下内容但在使用代码时出错:

  1. OPERE.FXDH.DEALDATE = TO_DATE(@var_date,''YYYY-MM-DD'')
  2. WHERE OPICS.FXDH.DEALDATE = CONVERT(DATETIME,@ var_date,120)
答案

正如我在评论中提到的,你需要使用动态sql。 OPENQUERY只接受文字字符串。

因此,您需要执行以下操作:

DECLARE @var_date date = GETDATE(); --Note the datatype change

DECLARE @SQL nvarchar(MAX);
SET @SQL = N'SELECT TOP(25) * FROM OPENQUERY (OPICS, ''SELECT CASE
            WHEN OPICS.FXDH.BR = 10 THEN ''Botswana''
            WHEN OPICS.FXDH.BR = 20 THEN ''Mozanmbique''
            WHEN OPICS.FXDH.BR = 30 THEN ''Rwanada''
            WHEN OPICS.FXDH.BR = 40 THEN ''Tanzania''
            WHEN OPICS.FXDH.BR = 50 THEN ''Zambia''
            WHEN OPICS.FXDH.BR = 60 THEN ''Zimbabwe''
       END AS COUNTRY,
       OPICS.FXDH.DEALNO,
       to_char(OPICS.FXDH.DEALDATE, ''YYYY-MM-DD'') AS Deal_Date,
       to_char(OPICS.FXDH.VDATE, ''YYYY-MM-DD'') AS V_Date,
       OPICS.FXDH.PS,
       OPICS.FXDH.CCY,
       OPICS.FXDH.CCYAMT,
       OPICS.FXDH.CCYRATE_8,
       OPICS.FXDH.CTRCCY,
       to_char(OPICS.OPER.OPERNAME) AS OPERNAME,
       to_char(OPICS.CUST.CFN1) AS CFN1,
       to_char(OPICS.PROD.PDESC) AS PDESC,
       to_char(OPICS.PORT.PORTDESC) AS Report_Description
FROM OPICS.FXDH AS FXDH
     INNER JOIN OPICS.OPER AS OPER ON (FXDH.BR = OPER.BR)
                                  AND (FXDH.TRAD = OPER.OPER)
     INNER JOIN OPICS.CUST AS CUST ON (FXDH.CUST = CUST.CNO)
     INNER JOIN OPICS.PROD AS PROD ON (FXDH.PRODCODE = PROD.PCODE)
     INNER JOIN OPICS.PORT AS PORT ON (FXDH.PORT = PORT.PORTFOLIO)
                                  AND (FXDH.BR = PORT.BR)
WHERE OPICS.FXDH.DEALDATE = ' + QUOTENAME(CONVERT(DATETIME , @var_date, 112),'''') + N'
  --dateString
  AND OPICS.FXDH.BR = 10;
--ORDER BY OPICS.FXDH.DEALDATE DESC'');';

--PRINT @SQL; --Your debugging best friend
EXEC sp_executesql @SQL;

注意我没有测试过这个,但我认为Oracle接受日期字符串文字的ISO格式。如果没有,您可能需要更改样式类型或在OPENQUERY中应用一些逻辑以将其更改为Oracle可接受的内容。

以上是关于在TO_DATE函数中使用变量的主要内容,如果未能解决你的问题,请参考以下文章

to_date 函数 AM PM 格式

Cx_oracle:在 python 中使用变量的 To_date

在 Oracle 中正确使用带日期的绑定变量?

to_date 一年不返回 4 位,只有 2

greenplum中to_date函数注意点

oracle中的to_date函数