Skip to Content
《SQL 烹饪书》第二版
book

《SQL 烹饪书》第二版

by Anthony Molinaro, Robert de Graaf
May 2025
Intermediate to advanced
570 pages
7h 38m
Chinese
O'Reilly Media, Inc.
Content preview from 《SQL 烹饪书》第二版

第 8 章 日期算术 日期算术

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章介绍执行简单日期运算的技巧。食谱涵盖了一些常见任务,如将天数加到日期上、查找日期之间的工作日数以及查找日期之间的天数差。

利用 RDBMS 的内置函数成功地处理日期,可以大大提高工作效率。在本章的所有示例中,我们都尽量利用 RDBMS 的内置函数。此外,我们还为所有配方选择了一种日期格式,即 DD-MON-YYYY。当然,还有其他一些常用格式,如 ISO 标准格式 DD-MM-YYYY。

我们选择将 DD-MON-YYYY 格式标准化,是为了让那些使用一种 RDBMS 并希望学习其他 RDBMS 的用户受益。使用一种标准格式将有助于您专注于每种 RDBMS 提供的不同技术和功能,而不必担心默认日期格式。

提示

本章主要介绍基本的红枣计算方法。下一章将介绍更高级的日期计算方法。本章介绍的公式使用简单的日期数据类型。如果使用更复杂的日期数据类型,则需要相应调整解决方案。

8.1 日、月、年的加减法

问题

您需要从日期中添加或减去一些天数、月数或年数。例如,使用雇员 CLARK 的 HIREDATE,您需要返回六个不同的日期:CLARK 被雇用前后的五天,CLARK 被雇用前后的五个月,以及 CLARK 被雇用前后的五年。CLARK 于 2006 年 6 月 9 日被录用,因此要返回以下结果集:

HD_MINUS_5D HD_PLUS_5D  HD_MINUS_5M HD_PLUS_5M  HD_MINUS_5Y HD_PLUS_5Y
----------- ----------- ----------- ----------- ----------- -----------
04-JUN-2006 14-JUN-2006 09-JAN-2006 09-NOV-2006 09-JUN-2001 09-JUN-2001
12-NOV-2006 22-NOV-2006 17-JUN-2006 17-APR-2007 17-NOV-2001 17-NOV-2001
18-JAN-2007 28-JAN-2007 23-AUG-2006 23-JUN-2007 23-JAN-2002 23-JAN-2002

解决方案

DB2

日期值允许标准加减法,但任何日期值的加减都必须在后面加上所代表的时间单位:

1 select hiredate -5 day   as hd_minus_5D,
2        hiredate +5 day   as hd_plus_5D,
3        hiredate -5 month as hd_minus_5M,
4        hiredate +5 month as hd_plus_5M,
5        hiredate -5 year  as hd_minus_5Y,
6        hiredate +5 year  as hd_plus_5Y
7   from emp
8  where deptno = 10

Oracle

使用标准加减法计算日,使用 ADD_MONTHS 函数计算月和年的加减法:

1 select hiredate-5 as hd_minus_5D, 2 hiredate+5 as hd_plus_5D, 3 add_months(hiredate,-5) as hd_minus_5M, 4 add_months(hiredate,5) as hd_plus_5M, 5 add_months(hiredate,-5*12) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

SQL Essentials For Dummies

SQL Essentials For Dummies

Richard Blum, Allen G. Taylor
Oracle SQL By Example

Oracle SQL By Example

Alice Rischert

Publisher Resources

ISBN: 9798341658813