[转]MONTHS_BETWEEN Function - Oracle to SQL Server Migration
本文转自: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的更多相关文章
- 使用Microsoft SQL Server Migration Assistant for Oracle迁移数据库
前言:使用Microsoft SQL Server Migration Assistant for Oracle迁移Oracle数据库到SqlServer数据库. 准备:Oracle11g.SqlSe ...
- InstallShield高级应用--检查是否安装ORACLE或SQL Server
InstallShield高级应用--检查是否安装ORACLE或SQL Server 实现原理:判断是否存在,是通过查找注册表是否含有相应标识来判断的. 注意:XP与WIN7系统注册表保存方式不一 ...
- ORACLE与SQL SERVER语法区别
一.数据类型 ORACLE与SQL SERVER在数据类型的对比如下: SQL SERVER ORACLE 数字类型 DECIMAL[(P[, S])] NUMBER[(P[, S])] NUMERI ...
- 对Oracle 、SQL Server、MySQL、PostgreSQL数据库优缺点分析
对Oracle .SQL Server.MySQL.PostgreSQL数据库优缺点分析 Oracle Database Oracle Database,又名Oracle RDBMS,或简称Oracl ...
- MySQL、Oracle和SQL Server的分页查询语句
假设当前是第PageNo页,每页有PageSize条记录,现在分别用Mysql.Oracle和SQL Server分页查询student表. 1.Mysql的分页查询: SELECT * FROM s ...
- Datatypes translation between Oracle and SQL Server
Datatypes translation between Oracle and SQL Server part 1: character, binary strings Datatypes tran ...
- Oracle与SQL SERVER编程差异分析(入门)
网上有关Oracle与SQL SERVER性能差异的文章很多,结论往往是让你根据数据量与预算来选择数据库.但实际项目中,特别是使用 .Net 开发的系统,支持以上两种数据库或者更多已经成为Boss的普 ...
- [Oracle][ODBC SQL Server Driver][SQL Server]对象名 'RECOVER.HS_TRANSACTION_LOG' 无效(转)
原帖由 qingyun 于 2010-6-21 15:44 发表 在写pl/sql的时候,有个很重要的注意点:比如:begin update 某个sqlserver的表@dblink名字 .... ...
- Oracle与SQL Server事务处理的比较
事务处理是所有大型数据库产品的一个关键问题,各数据库厂商都在这个方面花费了很大精力,不同的事务处理方式会导致数据库性能和功能上的巨大差异.事务处理也是数据库管理员与数据库应用程序开发人员必须深刻理解的 ...
随机推荐
- Asp.NetCore安全验证之JWT
本文只是介绍了下基于AspNetCore自带的System.IdentityModel.Tokens.Jwt.dll工具在项目中Token的应用. 我这里谈到的很浅显就两点: 一,超时时间 二,数据的 ...
- 当我们在谈论multidex65535时,我们在谈论什么
本文来自网易云社区 作者:郑文 首先我们并不在讨论车牌号.本文尽量避免谈论重复的技术点,只探讨一下multidex提供给我们的技术启示. 原理 multidex技术原理可以分成两个部分: 在app启动 ...
- Syncthing源码解析 - 在Gogland中对Syncthing的各个模块进行调试?
Syncthing的模块很多,各自负责不同的功能,如何能够对各个模块进行调试?Syncthing开发者早就想到这个问题了,允许开发者对任意模块进行单独调试,也允许同时对所有模块调试,调试方式是打印各个 ...
- Mybatis 的输入参数学习
mybatis 的输入参数: 指得就是parameterType的参数 这个参数就代表的sql语句中的输入参数 sql语句中的参数使用 有两种方式 : 使用#{} 获取: 8个基本数据类型 + S ...
- OC中NSClassFromString()与NSStringFromClass()的用法及应用场景
1.NSClassFromString()利用一个字符串创建一个类,我是在标签控制器中 UITabBarController中创建它的子控制器中使用的 - (void)viewDidLoad { [s ...
- 洛谷P2664 树上游戏(点分治)
传送门 题解 因为一个sb错误调了一个晚上……鬼晓得我为什么$solve(rt)$会写成$solve(v)$啊!!!一个$O(logn)$被我硬生生写成$O(n)$了竟然还能过$5$个点……话说还一直 ...
- python 使用eval() 可以将json格式的数据,转换为原始数据
使用python 自带的函数可以将json 格式的数据(也就是字符串)转换为原始格式的数据, 当使用json.loads()无法将json格式的数据转换为原始数据(存在多层各种格式类型数据的嵌套), ...
- python 取出字典的键或者值/如何删除一个字典的键值对/如何遍历字典
先定义一个字典并直接进行初始化赋值 my_dict = dict(name="lowman", age=45, money=998, hourse=None) 1.取出该字典所有的 ...
- 护网杯圆满结束,还不满足?不如来看看大佬的WP扩展思路~
护网杯预选赛 WP转载自:https://qingchenldl.github.io/2018/10/13/%E6%8A%A4%E7%BD%91%E6%9D%AFWP-BitPwn/#more WEB ...
- Linux动态库的导出控制
在实际工作中,许多软件模块是以动态库的方式提供的.做为模块开发人员,我们不仅要掌握如何编写和构建动态库,还要了解如何控制动态库的导出接口,这样,我们可以向模块的用户仅导出必要的接口,而另一些内部接口, ...