本文转自:http://www.sqlines.com/oracle-to-sql-server/months_between

In Oracle, MONTHS_BETWEEN(date1, date2) function returns the number of months between two dates as a decimal number.

Note that SQL Server DATEDIFF(month, date2, date1) function does not return exactly the same result, and you have to use an user-defined function if you need to fully emulate the Oracle MONTHS_BETWEEN function (see UDF's code below).

Oracle:

  ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';
 
-- 1-day difference
SELECT MONTHS_BETWEEN('2013-03-01', '2013-02-28') FROM dual;
# 0.129032258
 
-- Still 1-day difference but the result is different
SELECT MONTHS_BETWEEN('2013-03-02', '2013-03-01') FROM dual;
# 0.32258065

SQL Server:

DATEDIFF always returns an integer result.

  -- 1-day difference, but 1 month returned (!)
SELECT DATEDIFF(month, '2013-02-28', '2013-03-01');
# 1
 
-- Still 1-day difference but the result is different
SELECT DATEDIFF(month, '2013-03-01', '2013-03-02');
# 0

Also note that MONTHS_BETWEEN and DATEDIFF have different order of parameters.

Oracle MONTHS_BETWEEN in Detail

MONTHS_BETWEEN returns the number of full months between dates and a fractional part.

An integer value is returned only if:

  • Both dates specify the same day of the month (February 13 and March 13 i.e.)
  • Both dates are the last days of the months (January 31 and April 30 i.e.)

Oracle:

  -- Between March 13 and February 13
SELECT MONTHS_BETWEEN('2013-03-13', '2013-02-13') FROM dual;
# 1
 
-- Between April 30 and January 31
SELECT MONTHS_BETWEEN('2013-04-30', '2013-01-31') FROM dual;
# 3

Fractional Part

The fractional part is calculated using the following formula:

Condition Fractional Part Calculation
If day_of_date1 > day_of_date2 (day_of_date1 - day_of_date2) / 31
If day_of_date1 < day_of_date2 (31 - day_of_date2 + day_of_date1) / 31

Note that when MONTHS_BETWEEN calculates the fractional part, it considers that all months have 31 days.

Consider the following examples:

Oracle:

 -- 1-day difference
SELECT MONTHS_BETWEEN('2013-03-01', '2013-02-28') FROM dual;
# 0.129032258

Although there is just 1-day difference between February 28, 2013 and March 01, 2013, MONTHS_BETWEEN considers Feb 29, Feb 30, Feb 31 and Mar 01:

(31 - 28 + 1) / 31 =  0.129032258

Another example:

  -- Still 1-day difference but the result is different
SELECT MONTHS_BETWEEN('2013-03-02', '2013-03-01') FROM dual;
# 0.32258065

Now the fractional part is calculated as follows:

(2 - 1) / 31 = 0.32258065

SQL Server User-Defined Function to Emulate Oracle MONTHS_BETWEEN

You can use the following user-defined function to emulate Oracle MONTHS_BETWEEN function:

SQL Server:

   CREATE FUNCTION MONTHS_BETWEEN (@date1 DATETIME, @date2 DATETIME)
RETURNS FLOAT
AS
/******************************************************************************
PURPOSE: Emulate Oracle MONTHS_BETWEEN in SQL Server
 
REVISIONS:
Ver Date Author Description
--------- ---------- --------------- ---------------------------
1.1 2013-02-10 Dmitry Tolpeko (SQLines) Created.
******************************************************************************/
BEGIN
DECLARE @months FLOAT = DATEDIFF(month, @date2, @date1);
 
-- Both dates does not point to the same day of month
IF DAY(@date1) <> DAY(@date2) AND
-- Both dates does not point to the last day of month
(MONTH(@date1) = MONTH(@date1 + 1) OR MONTH(@date2) = MONTH(@date2 + 1))
BEGIN
-- Correct to include full months only and calculate fraction
IF DAY(@date1) < DAY(@date2)
SET @months = @months + CONVERT(FLOAT, 31 - DAY(@date2) + DAY(@date1)) / 31 - 1;
ELSE
SET @months = @months + CONVERT(FLOAT, DAY(@date1) - DAY(@date2)) / 31;
END
 
RETURN @months;
END;
GO

Now you can use the UDF as follows:

SQL Server:

    -- 1-day difference
SELECT dbo.MONTHS_BETWEEN('2013-03-01', '2013-02-28');
# 0.129032258
 
-- Still 1-day difference but the result is different (as in Oracle)
SELECT dbo.MONTHS_BETWEEN('2013-03-02', '2013-03-01');
# 0.32258065

[转]MONTHS_BETWEEN Function - Oracle to SQL Server Migration的更多相关文章

  1. 使用Microsoft SQL Server Migration Assistant for Oracle迁移数据库

    前言:使用Microsoft SQL Server Migration Assistant for Oracle迁移Oracle数据库到SqlServer数据库. 准备:Oracle11g.SqlSe ...

  2. InstallShield高级应用--检查是否安装ORACLE或SQL Server

    InstallShield高级应用--检查是否安装ORACLE或SQL Server   实现原理:判断是否存在,是通过查找注册表是否含有相应标识来判断的. 注意:XP与WIN7系统注册表保存方式不一 ...

  3. ORACLE与SQL SERVER语法区别

    一.数据类型 ORACLE与SQL SERVER在数据类型的对比如下: SQL SERVER ORACLE 数字类型 DECIMAL[(P[, S])] NUMBER[(P[, S])] NUMERI ...

  4. 对Oracle 、SQL Server、MySQL、PostgreSQL数据库优缺点分析

    对Oracle .SQL Server.MySQL.PostgreSQL数据库优缺点分析 Oracle Database Oracle Database,又名Oracle RDBMS,或简称Oracl ...

  5. MySQL、Oracle和SQL Server的分页查询语句

    假设当前是第PageNo页,每页有PageSize条记录,现在分别用Mysql.Oracle和SQL Server分页查询student表. 1.Mysql的分页查询: SELECT * FROM s ...

  6. Datatypes translation between Oracle and SQL Server

    Datatypes translation between Oracle and SQL Server part 1: character, binary strings Datatypes tran ...

  7. Oracle与SQL SERVER编程差异分析(入门)

    网上有关Oracle与SQL SERVER性能差异的文章很多,结论往往是让你根据数据量与预算来选择数据库.但实际项目中,特别是使用 .Net 开发的系统,支持以上两种数据库或者更多已经成为Boss的普 ...

  8. [Oracle][ODBC SQL Server Driver][SQL Server]对象名 'RECOVER.HS_TRANSACTION_LOG' 无效(转)

    原帖由 qingyun 于 2010-6-21 15:44 发表 在写pl/sql的时候,有个很重要的注意点:比如:begin  update  某个sqlserver的表@dblink名字 .... ...

  9. Oracle与SQL Server事务处理的比较

    事务处理是所有大型数据库产品的一个关键问题,各数据库厂商都在这个方面花费了很大精力,不同的事务处理方式会导致数据库性能和功能上的巨大差异.事务处理也是数据库管理员与数据库应用程序开发人员必须深刻理解的 ...

随机推荐

  1. pageadmin网站制作 怎么验证sql用户名和密码的正确性

    使用pageadmin建站系统的时候,不懂可以参考官网教程. 1.打开SQL Server Management Studio会弹出如下界面. 第一个箭头指向的就是服务器名称,如果用ip无法连接sql ...

  2. CF834D The Bakery

    题目链接:戳我 题意:将一个长度为n的序列分为k段,使得总价值最大.一段区间的价值表示为区间内不同数字的个数 \(n<=35000,k<=50\) 开始想的转移方程是这个样子的--\(dp ...

  3. kvm快照备份及常用命令

    转载自:http://www.myjishu.com/?p=431 好文章 kvm快照备份及常用命令 kvm快照,分两种: 1种lvm快照,如果分区是lvm,可以利用lvm进行kvm的快照备份 2种由 ...

  4. 2018年Android面试题含答案--适合中高级(上)

    这些面试题是我在今年年初换工作的时候整理,没有重点.包括java基础,数据结构,网络,Android相关等等.适合中高级工程师.由于内容过多,将会分为上下两部分.下部分跳转链接:http://www. ...

  5. 【vim】插入模式与常用编辑操作

    vim不像很多编辑器那样一启动便可以直接编辑文本,需要在普通模式按下i, a等键才会进入插入模式进行文本编辑. 如何进入插入模式 以下的命令都会让vim从普通模式切换到插入模式,但命令执行后的字符插入 ...

  6. winserver2008安装tomcat+mysql+httpd+redis环境

    1. 装tomcat和jdk http://www.cnblogs.com/SHI520/p/4546849.html     2. 安装mysql5.7 https://www.jb51.net/a ...

  7. Azure Powershell部署使用平台映像的托管Windows VM及相关问题说明

    1.脚本背景信息: a.使用平台镜像(Windows Server 2016 zh-cn)部署高性能托管磁盘虚拟机 b.虚拟机默认不开启Boot诊断 c.添加三块已经创建好的数据磁盘 d.添加已创建好 ...

  8. php脚本cli 模式运行

    参考文章 http://rapheal.sinaapp.com/2013/11/20/php_zend_hello_world/ http://www.douban.com/note/33788568 ...

  9. 前端知识总结--js原型链

    js的原型链听着比较深奥,看着容易晕,梳理一下还是比较容易懂的 (先简单写下,后续有时间再整理) 简而言之 原型链:就是js的对象与对象之间,通过原型组成建立的层层关系,构成了整个链条,称之为原型链  ...

  10. Vue 不睡觉教程3 - 来点实在的:自动计算剩余时间的任务列表

    目标前两课教的是入门和文件结构.都没有什么实在的东西.这次我们要来点实在的.我们要做出一个待办列表.这个待办列表有以下特点: 可以自动从文本中抽取出这件事情的开始时间可以显示当前距离这件事情的开始时间 ...