简体   繁体   English

将 oracle 查询的日期格式转换为 sql 服务器查询

[英]convert date format of oracle query to sql server query

i am trying to convert below oracle query to SQL server query.我正在尝试将 oracle 查询转换为 SQL 服务器查询。

Select * from table A
WHERE  TRUNC(a.Generate_DATE,'MM') = (SELECT (NVL (TRUNC (TO_DATE ('$$date','MM/DD/YYYY HH24:MI:SS')),TRUNC(ADD_MONTHS(SYSDATE, -1),'MM'))) FROM DUAL)

where $$date is parameter value and will be passed from outside the query.其中 $$date 是参数值,将从查询外部传递。

The issue is with dateformat.I am unable to convert this exact format in sql server.问题出在 dateformat 上。我无法在 sql 服务器中转换这种确切的格式。

To get the first day of the month use dateadd and day , eg要获取当月的第一天,请使用dateaddday ,例如

declare @d date = getdate()
select dateadd(day,  1-day(@d), @d)

For NVL use the standard coalesce function.对于NVL ,使用标准合并function。

For TRUNC use cast(getdate() as date)对于TRUNC使用cast(getdate() as date)

For ADD_MONTHS(SYSDATE, -1) use dateadd(month,-1,getdate())对于ADD_MONTHS(SYSDATE, -1)使用dateadd(month,-1,getdate())

or go get the SQL Server Migration Assistant for Oracle , which will translate Oracle SQL and PL/SQL to TSQL for you. or go get the SQL Server Migration Assistant for Oracle , which will translate Oracle SQL and PL/SQL to TSQL for you. It also installs a bunch of compatibility functions so with SSMA this is translated to:它还安装了一堆兼容性功能,因此使用 SSMA 将其转换为:

WHERE ssma_oracle.trunc_date2(A.END_DATE, 'MM') = 
   (
      SELECT isnull(ssma_oracle.trunc_date(ssma_oracle.to_date2('12/01/2020 18:15:00', 'MM/DD/YYYY HH24:MI:SS')), ssma_oracle.trunc_date2(dateadd(m, -1, sysdatetime()), 'MM')) AS expr
   )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM