[转]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事务处理的比较
事务处理是所有大型数据库产品的一个关键问题,各数据库厂商都在这个方面花费了很大精力,不同的事务处理方式会导致数据库性能和功能上的巨大差异.事务处理也是数据库管理员与数据库应用程序开发人员必须深刻理解的 ...
随机推荐
- unity 序列化和反序列化
什么是序列化和反序列化(1)序列化是指把对象转换为字节序列的过程,而反序列化是指把字节序列恢复为对象的过程:. (2)序列化:对象序列化的最主要的用处就是在传递和保存对象的时候,保证对象的完整性和可 ...
- C#基础笔记(第二十天)
1.复习属性:保护字段的构造函数:初始化对象初始化对象:给对象的每个属性去赋值什么时候会调用构造函数:当我们new的时候面向对象中需要注意的两个关键字this 1.代表当前类的对象 2.调用自己的构造 ...
- JavaWeb -pageContext/request/session/application
pageContext/request/session/application总结 一.范围差异 1. pageContext jsp页面容器 当前页面有效 2. request 请求对象 同一次请求 ...
- JAVA Double去掉科学计数"E"
当Double的值很大时,显示的结果会变成带E的科学计数法显示,在报表的数据显示的时候不方便阅读,需要去掉E,将原数据显示 public static void main(String[] args) ...
- Usboot V1.68版本
V1.68版本,我的收藏之一 官方的介绍: 市面上现在大多数U盘都支持启动机器的功能,但是要制作启动型U盘,需要进入WIN98,现在很多人机器 上都没有98了吧,呵呵.为了做个启动盘,装一个98,多冤 ...
- [Objective-C语言教程]类别(28)
有时,可能会发现希望通过添加仅在某些情况下有用的行为来扩展现有类. 要向现有类添加此类扩展,Objective-C提供了类别和扩展. 如果需要向现有类添加方法,或许为了添加功能以便在应用程序中更容易地 ...
- mxonline实战6 , 忘记用户密码时进行重置
对应github地址:密码重置 原理: 1. 一个需要输入用户邮箱和注册码的密码忘记页面 2. 点击提交后,用户邮箱收到一个邮件,包含重置密码的链接 3. 点击链接进入密码重置页面 ...
- SpringMvc HttpMessageConverter之@ResponseBody
我们先看HttpMessageConverter的示意图,从图片可以看出它是多么的重要.在一条必经之路截道了的感觉. 先上我的测试例子: jsp页面: <%@ page language=&qu ...
- 【Qt开发】实现系统托盘,托盘菜单,托盘消息
概述 系统托盘就是在系统桌面底部特定的区域显示运行的程序.windows在任务栏状态区域,linux在布告栏区域.应用程序系统托盘功能,是比较普遍的功能,本篇将详细的介绍如何实现该功能. 演示Demo ...
- FTP在docker容器中上传失败解决,改为被动模式
package com.mayocase.takeout.utils; import org.apache.commons.net.ftp.FTPClient; import org.apache.c ...