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 烹饪书》第二版

第 9 章 日期操纵 日期操纵

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

本章介绍搜索和修改日期的方法。涉及日期的查询非常常见。因此,在处理日期时,你需要知道如何思考,并充分了解 RDBMS 平台提供的用于操作日期的函数。本章中的秘诀为今后的工作奠定了重要基础,因为今后你不仅可以使用日期查询,还可以使用时间查询等更复杂的查询。

在介绍食谱之前,我们想强调一个概念(在前言中提到),即把这些解决方案作为解决具体问题的指南。尽量从 "大局 "考虑问题。例如,如果一个食谱解决了当前月份的问题,请记住,你可以在任何月份使用这个食谱(稍作修改),而不仅仅是食谱中使用的月份。再次强调,这些食谱只是指南,绝对是最终选择。一本书不可能包含你所有问题的答案,但如果你理解了这里介绍的内容,修改这些解决方案以满足你的需求是轻而易举的。还可以考虑这些解决方案的其他版本。例如,如果解决方案使用的是 RDBMS 提供的一个特定函数,那么就值得花时间和精力去了解是否有替代方案--也许比这里介绍的方案效率更高,也许更低。了解你的选择会让你成为一个更好的 SQL 程序员。

提示

本章介绍的配方使用简单的日期数据类型。如果使用更复杂的日期数据类型,则需要相应调整解决方案。

9.1 确定年份是否为闰年

问题

您想确定当前年份是否为闰年。

解决方案

如果你在 SQL 上工作过一段时间,那么毫无疑问,你已经接触过几种解决这个问题的技术。我们遇到过的几乎所有解决方案都很有效,但本食谱中介绍的可能是最简单的一种。该解决方案只需检查二月的最后一天;如果是 29 日,则当前年份是闰年。

DB2

使用递归 WITH 子句返回二月份的每一天。使用聚合函数 MAX 确定二月份的最后一天:

 1   with x (dy,mth)
 2     as (
 3 select dy, month(dy)
 4   from (
 5 select (current_date -
 6          dayofyear(current_date) days +1 days)
 7           +1 months as dy
 8   from t1
 9        ) tmp1
10  union all
11 select dy+1 days, mth
12   from x
13  where month(dy+1 day) = mth
14 )
15 select max(day(dy))
16   from x

Oracle

使用函数 LAST_DAY 查找二月份的最后一天:

1 select to_char(
2          last_day(add_months(trunc(sysdate,'y'),1)),
3         'DD')
4   from t1

PostgreSQL

使用函数 GENERATE_SERIES 返回二月份的每一天,然后使用集合函数 MAX 找出二月份的最后一天:

 1 select max(to_char(tmp2.dy+x.id,'DD')) as dy 2 from ( 3 select dy, to_char(dy,'MM') as mth 4 from ( 5 select cast(cast( 6 date_trunc('year',current_date) as date) 7 + interval '1 month' as date) as dy 8 from t1 9 ) tmp1 10 ) tmp2, generate_series (0,29) x(id) 11 where ...
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