选读SQL经典实例笔记06_日期处理(上)

news2024/10/6 6:46:20

 

1. 计算一年有多少天

1.1. 方案

1.1.1. 找到当前年份的第一天

1.1.2. 加上1年以得到下一年的第一天

1.1.3. 得到的结果减去第一步得到的结果

1.2. DB2

1.2.1.  sql

select days((curr_year + 1 year)) - days(curr_year)
   from (
 select (current_date -
         dayofyear(current_date) day +
          1 day) curr_year
   from t1
        ) x

1.3. Oracle

1.3.1.  sql

select add_months(trunc(sysdate,'y'),12) - trunc(sysdate,'y')
   from dual

1.4. PostgreSQL

1.4.1.  sql

select cast((curr_year + interval '1 year') as date) - curr_year
   from (
 select cast(date_trunc('year',current_date) as date) as curr_year
   from t1
        ) x

1.5. MySQL

1.5.1.  sql

select datediff((curr_year + interval 1 year),curr_year)
   from (
 select adddate(current_date,-dayofyear(current_date)+1) curr_year
   from t1
        ) x

1.6. SQL Server

1.6.1.  sql

select datediff(d,curr_year,dateadd(yy,1,curr_year))
   from (
 select dateadd(d,-datepart(dy,getdate())+1,getdate()) curr_year
   from t1
        ) x

2. 日期值里提取年月日时分秒

2.1. DB2

2.1.1.  sql

select   hour( current_timestamp ) hr,
        minute( current_timestamp ) min,
        second( current_timestamp ) sec,
           day( current_timestamp ) dy,
         month( current_timestamp ) mth,
          year( current_timestamp ) yr
   from t1

2.2. Oracle

2.2.1.  sql

select to_number(to_char(sysdate,'hh24')) hour,
        to_number(to_char(sysdate,'mi')) min,
        to_number(to_char(sysdate,'ss')) sec,
        to_number(to_char(sysdate,'dd')) day,
        to_number(to_char(sysdate,'mm')) mth,
        to_number(to_char(sysdate,'yyyy')) year
    from dual

2.3. PostgreSQL

2.3.1.  sql

select to_number(to_char(current_timestamp,'hh24'),'99') as hr,
        to_number(to_char(current_timestamp,'mi'),'99') as min,
        to_number(to_char(current_timestamp,'ss'),'99') as sec,
        to_number(to_char(current_timestamp,'dd'),'99') as day,
        to_number(to_char(current_timestamp,'mm'),'99') as mth,
        to_number(to_char(current_timestamp,'yyyy'),'9999') as yr
   from t1

2.4. MySQL

2.4.1.  sql

select date_format(current_timestamp,'%k') hr,
        date_format(current_timestamp,'%i') min,
        date_format(current_timestamp,'%s') sec,
        date_format(current_timestamp,'%d') dy,
        date_format(current_timestamp,'%m') mon,
        date_format(current_timestamp,'%Y') yr
   from t1

2.5. SQL Server

2.5.1.  sql

select datepart( hour, getdate()) hr,
        datepart( minute,getdate()) min,
        datepart( second,getdate()) sec,
        datepart( day,   getdate()) dy,
        datepart( month, getdate()) mon,
        datepart( year, getdate()) yr
   from t1

3. 一个月的第一天和最后一天

3.1. DB2

3.1.1.  sql

select (current_date - day(current_date) day +1 day) firstday,
        (current_date +1 month -day(current_date) day) lastday
   from t1

3.2. Oracle

3.2.1.  sql

select trunc(sysdate,'mm') firstday,
        last_day(sysdate) lastday
   from dual

3.3. PostgreSQL

3.3.1.  sql

select firstday,
        cast(firstday + interval '1 month'
                      - interval '1 day' as date) as lastday
   from (
 select cast(date_trunc('month',current_date) as date) as firstday
   from t1
        ) x

3.4. MySQL

3.4.1.  sql

select date_add(current_date,
                 interval -day(current_date)+1 day) firstday,
        last_day(current_date) lastday
   from t1

3.5. SQL Server

3.5.1.  sql

select dateadd(day,-day(getdate())+1,getdate()) firstday,
       dateadd(day,
               -day(getdate( )),
               dateadd(month,1,getdate())) lastday
   from t1

4. 当前月份的第一个和最后一个星期一

4.1. DB2

4.1.1.     sql

with x (dy,mth,is_monday)
       as (
  select dy,month(dy),
          case when dayname(dy)='Monday'
               then 1 else 0
          end
     from (
   select (current_date-day(current_date) day +1 day) dy
     from t1
          ) tmp1
   union all
  select (dy +1 day), mth,
         case when dayname(dy +1 day)='Monday'
               then 1 else 0
         end
    from x
   where month(dy +1 day) = mth
  )
  select min(dy) first_monday, max(dy) last_monday
    from x
   where is_monday = 1

4.2. Oracle

4.2.1. sql

select next_day(trunc(sysdate,'mm')-1,'MONDAY') first_monday,
       next_day(last_day(trunc(sysdate,'mm'))-7,'MONDAY') last_monday
  from dual

4.3. PostgreSQL

4.3.1.   sql

select first_monday,
          case to_char(first_monday+28,'mm')
               when mth then first_monday+28
                        else first_monday+21
          end as last_monday
     from (
   select case sign(cast(to_char(dy,'d') as integer)-2)
               when  0
               then dy
              when -1
              then dy+abs(cast(to_char(dy,'d') as integer)-2)
              when 1
              then (7-(cast(to_char(dy,'d') as integer)-2))+dy
         end as first_monday,
         mth
    from (
  select cast(date_trunc('month',current_date) as date) as dy,
         to_char(current_date,'mm') as mth
    from t1
         ) x
         ) y

4.4. MySQL

4.4.1.   sql

select first_monday,
          case month(adddate(first_monday,28))
               when mth then adddate(first_monday,28)
                        else adddate(first_monday,21)
          end last_monday
     from (
   select case sign(dayofweek(dy)-2)
               when 0 then dy
               when -1 then adddate(dy,abs(dayofweek(dy)-2))
              when 1 then adddate(dy,(7-(dayofweek(dy)-2)))
         end first_monday,
         mth
    from (
  select adddate(adddate(current_date,-day(current_date)),1) dy,
         month(current_date) mth
    from t1
         ) x
         ) y

4.5. SQL Server

4.5.1.     sql

with x (dy,mth,is_monday)
       as (
   select dy,mth,
          case when datepart(dw,dy) = 2
               then 1 else 0
          end
     from (
   select dateadd(day,1,dateadd(day,-day(getdate()),getdate())) dy,
          month(getdate()) mth
    from t1
         ) tmp1
   union all
  select dateadd(day,1,dy),
         mth,
         case when datepart(dw,dateadd(day,1,dy)) = 2
              then 1 else 0
         end
    from x
   where month(dateadd(day,1,dy)) = mth
  )
  select min(dy) first_monday,
         max(dy) last_monday
    from x
   where is_monday = 1

5. 一年中所有的星期五

5.1. DB2

5.1.1.     sql

with x (dy,yr)
       as (
   select dy, year(dy) yr
     from (
   select (current_date -
            dayofyear(current_date) days +1 days) as dy
     from t1
           ) tmp1
    union all
  select dy+1 days, yr
    from x
   where year(dy +1 day) = yr
  )
  select dy
    from x
   where dayname(dy) = 'Friday'

5.2. Oracle

5.2.1.     sql

with x
       as (
   select trunc(sysdate,'y')+level-1 dy
     from t1
     connect by level <=
        add_months(trunc(sysdate,'y'),12)-trunc(sysdate,'y')
   )
   select *
     from x
   where to_char( dy, 'dy') = 'fri'

5.3. PostgreSQL

5.3.1.  sql

select cast(date_trunc('year',current_date) as date)
          + x.id as dy
    from generate_series (
          0,
          ( select cast(
                   cast(
             date_trunc('year',current_date) as date)
                        + interval '1 years' as date)
                        - cast(
                   date_trunc('year',current_date) as date) )-1
         ) x(id)
  where to_char(
           cast(
     date_trunc('year',current_date)
                as date)+x.id,'dy') = 'fri'

5.4. MySQL

5.4.1.   sql

select dy
     from (
   select adddate(x.dy,interval t500.id-1 day) dy
     from (
   select dy, year(dy) yr
     from (
   select adddate(
          adddate(current_date,
                  interval -dayofyear(current_date) day),
                 interval 1 day ) dy
    from t1
         ) tmp1
         ) x,
         t500
   where year(adddate(x.dy,interval t500.id-1 day)) = x.yr
         ) tmp2
   where dayname(dy) = 'Friday'

5.5. SQL Server

5.5.1.     sql

with x (dy,yr)
       as (
   select dy, year(dy) yr
     from (
   select getdate()-datepart(dy,getdate())+1 dy
      from t1
           ) tmp1
    union all
   select dateadd(dd,1,dy), yr
   from x
   where year(dateadd(dd,1,dy)) = yr
  )
  select x.dy
    from x
   where datename(dw,x.dy) = 'Friday'
  option (maxrecursion 400)

6. 判断闰年

6.1. DB2

6.1.1.  sql

 with x (dy,mth)
     as (
   select dy, month(dy)
     from (
   select (current_date -
            dayofyear(current_date) days +1 days)
             +1 months as dy
     from t1
           ) tmp1
   union all
  select dy+1 days, mth
    from x
   where month(dy+1 day) = mth
  )
  select max(day(dy))
    from x

6.2. Oracle

6.2.1.  sql

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

6.3. PostgreSQL

6.3.1.   sql

select max(to_char(tmp2.dy+x.id,'DD')) as dy
     from (
   select dy, to_char(dy,'MM') as mth
     from (
   select cast(cast(
               date_trunc('year',current_date) as date)
                          + interval '1 month' as date) as dy
     from t1
           ) tmp1
          ) tmp2, generate_series (0,29) x(id)
    where to_char(tmp2.dy+x.id,'MM') = tmp2.mth

6.4. MySQL

6.4.1.  sql

select day(
         last_day(
         date_add(
         date_add(
         date_add(current_date,
                  interval -dayofyear(current_date) day),
                  interval 1 day),
                  interval 1 month))) dy
   from t1

6.5. SQL Server

6.5.1.     sql

with x (dy,mth)
       as (
   select dy, month(dy)
     from (
   select dateadd(mm,1,(getdate()-datepart(dy,getdate()))+1) dy
     from t1
          ) tmp1
    union all
   select dateadd(dd,1,dy), mth
    from x
   where month(dateadd(dd,1,dy)) = mth
  )
  select max(day(dy))
    from x

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/748210.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数字电路设计——流水线处理器

数字电路设计——流水线处理器 从内存或是寄存器组读写、操作ALU是处理器中最耗时的操作。我们将一个指令拆成五个流水线阶段,每个阶段同时执行耗时的组合逻辑。这五个阶段分别是取指令、解码、执行、访存和写回。 在取指令阶段,处理器从指令存储器中读…

python -m 是什么命令

python -m 命令是什么意思 首先python --help 可以看到-m的含义:意思是将库中的python模块用作脚本去运行。 python --help 命令显示结果 python -m xxx.py 和python xxx.py 有什么区别 这是两种加载py文件的方式: 叫做直接运行(python xxx.py&#xf…

Android 音频可视化:频谱特效的探索与实践

音频可视化,一言以蔽之,就是声音到图像的转换。 随着视觉工业时代的到来,用户逐渐重视产品的极致体验,在市场上诸多优秀的音乐类APP中,频谱动效 是一个经典的应用场景: 图片来源:咪咕音乐 本文…

【C++】C++的IO流

目录 一、C语言的输入与输出 二、流的概念 三、CIO流 1、C标准IO流 2、C文件IO流 四、stringstream的简单介绍 一、C语言的输入与输出 C语言中我们用到的最频繁的输入输出方式就是scanf ()与printf()。 scanf(): 从标准输入设备(键盘)读取数据,并将值存放在变量…

项目管理:如何减少项目中的信息差

在项目管理中,信息差的存在是难以避免的。 一方面,由于项目干系人各自的角色不同,项目经理不可能让每个人都知道所有的事情,以免在信息的海洋中产生更多的干扰。 另一方面,项目干系人需要了解项目的情况&#xff0c…

华为OD机试真题 Java 实现【数字序列比大小】【2023 B卷 100分】,田忌赛马,永远比你大,你服不服?

目录 一、题目描述二、输入描述三、输出描述四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明 大家好,我是哪吒。 做技术,我是认真的,立志于打造最权威的华为OD机试真题专栏,帮助那些与我有同样需求的人&#xff…

ES6——Promise

promise 含义:异步编程解决方案 特点:1、状态不受外界影响,状态有三种:pending、fulfilled、rejected 2、状态不可逆,只能pending -> fulfilled、pending -> rejected 缺点:无法取消、不设置回调函…

常见的人体静电消除器的工作原理

人体静电消除器是一种用于消除人体带有静电的装置。静电是指物体表面具有电荷的现象,当人体带有静电时,会导致一些不舒适的感觉,同时也容易引起电击事故。 人体静电消除器的工作原理主要通过以下几个方面来实现: 1.接地&#xf…

深入篇【Linux】学习必备:认识冯诺依曼系统+理解操作系统(‘‘管理‘‘)

深入篇【Linux】学习必备:认识冯诺依曼系统理解操作系统(管理) Ⅰ.冯诺依曼系统结构1.特点(what)2.理解(why)3.案例(how) Ⅱ.操作系统概念与定位1.概念(what)2.设计OS目的(why)3.管理(how) Ⅰ.冯诺依曼系统结构 1.特点(what) 我们认识的计算机&#xff…

ETHERCAT转ETHERCAT网关三菱plc支持ethercat吗

大家好,今天要和大家分享一款神器——远创智控YC-ECAT-ECAT通讯网关!这款网关有什么厉害的呢?且听我慢慢道来。 首先,YC-ECAT-ECAT是一款自主研发的ETHERCAT从站功能的通讯网关。那什么是ETHERCAT呢?简单来说&#xff…

揭秘ChatGPT的流式返回

107. 揭秘ChatGPT的流式返回 ChatGPT是一种强大的语言模型,可以生成自然语言响应。在传统的请求/响应模型中,客户端发送请求,服务器处理请求后返回响应。但是,使用流式返回可以实现持续的数据流,使得客户端能够实时接…

水文水动力模型在城市内涝、城市排水、海绵城市规划设计中深度应用

随着计算机的广泛应用和各类模型软件的发展,将排水系统模型作为城市洪灾评价与防治的技术手段已经成为防洪防灾的重要技术途径。将聚焦于综合利用GIS及CAD等工具高效地进行大规模城市排水系统水力模型的建立,利用SWMM实现排水系统水力模拟。讲解SWMM深度…

rabbitmq使用springboot实现fanout模式

一、fanout模式 类型&#xff1a;fanout特点&#xff1a;Fanout—发布与订阅模式&#xff0c;是一种广播机制&#xff0c;它是没有路由key的模式。 二、实现 1、引入相应的pom文件 pom.xml <?xml version"1.0" encoding"UTF-8"?> <project x…

左神算法 重要技巧:递归的加速技巧(斐波那契数列套路)以及推广

目录 【案例1】【十分重要 &#xff1a; 斐波那契递归套路&#xff0c;只要像斐波那契这种严格递归都可以进行类似的优化】 【有严格的递归项后&#xff0c;通过线性代数的知识进行优化】 【代码实现】 【技巧推广】 【实例1 使用这个技巧】 【题目描述】 【思路解析】 …

求两个数的最大值max

函数实现 int max(int a, int b); 函数接收两个整数参数&#xff0c;在内部用if语句判断哪个大&#xff0c;返回大的即可。 完整代码 #include <iostream> using namespace std;int max(int a, int b) {if (a > b){return a;}else{return b;} }int main() {int n1…

开发工具篇第25讲:阿里云MFA绑定Chrome浏览器Authenticator插件

开发工具篇第25讲&#xff1a;阿里云MFA绑定Chrome浏览器Authenticator插件 本文是开发工具篇第25讲&#xff0c;登录阿里云旗下产品时&#xff0c;需要使用mfa登录&#xff0c;每次如果要用手机看mfa码很麻烦&#xff0c; Chrome浏览器提供了一个快捷的登录方法&#xff0c;可…

软件工程师,学习下JavaScript ES6新特性吧

概述 作为一名软件工程师&#xff0c;不管你是不是前端开发的岗位&#xff0c;工作中或多或少都会用到一点JavaScript。JavaScript是大家所了解的语言名称&#xff0c;但是这个语言名称是Oracle公司注册的商标。JavaScript的正式名称是ECMAScript。1996年11月&#xff0c;JavaS…

Mysql根据积分排名

积分表&#xff1a;t_participant_points 1、带并列 SELECT p.*, CASE WHEN prevRank p.total_points THEN curRank WHEN prevRank : p.total_points THEN curRank : curRank 1 END AS ranking FROM ( SELECT total_points …

LabVIEW-实现波形发生器

一、题目 用两种方法实现一种多类型信号波形发生器&#xff08;至少包括&#xff1a;正弦波、三角波、方波等&#xff09;&#xff0c;可以调节信号频率、幅度、相位等参数&#xff0c;可以图形化显示信号波形。 需要给出产生信号波形的基本方法、程序设计基本方法以及程序实现…

931.下降路径最小和

931.下降路径最小和 给你一个 n x n 的 方形 整数数组 matrix &#xff0c;请你找出并返回通过 matrix 的下降路径 的 最小和 。 下降路径 可以从第一行中的任何元素开始&#xff0c;并从每一行中选择一个元素。在下一行选择的元素和当前行所选元素最多相隔一列&#xff08;即…