主頁 > 資料庫 > 選讀SQL經典實體筆記06_日期處理(上)

選讀SQL經典實體筆記06_日期處理(上)

2023-07-13 08:35:14 資料庫

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

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/557178.html

標籤:其他

上一篇:【技識訓累】Mysql中的SQL語言【技術篇】【二】

下一篇:返回列表

標籤雲
其他(162503) Python(38274) JavaScript(25532) Java(18294) C(15241) 區塊鏈(8275) C#(7972) AI(7469) 爪哇(7425) MySQL(7299) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5876) 数组(5741) R(5409) Linux(5347) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4616) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2439) ASP.NET(2404) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) HtmlCss(2002) .NET技术(1987) 功能(1967) Web開發(1951) C++(1942) python-3.x(1918) 弹簧靴(1913) xml(1889) PostgreSQL(1884) .NETCore(1863) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • GPU虛擬機創建時間深度優化

    **?桔妹導讀:**GPU虛擬機實體創建速度慢是公有云面臨的普遍問題,由于通常情況下創建虛擬機屬于低頻操作而未引起業界的重視,實際生產中還是存在對GPU實體創建時間有苛刻要求的業務場景。本文將介紹滴滴云在解決該問題時的思路、方法、并展示最終的優化成果。 從公有云服務商那里購買過虛擬主機的資深用戶,一 ......

    uj5u.com 2020-09-10 06:09:13 more
  • 可編程網卡芯片在滴滴云網路的應用實踐

    **?桔妹導讀:**隨著云規模不斷擴大以及業務層面對延遲、帶寬的要求越來越高,采用DPDK 加速網路報文處理的方式在橫向縱向擴展都出現了局限性。可編程芯片成為業界熱點。本文主要講述了可編程網卡芯片在滴滴云網路中的應用實踐,遇到的問題、帶來的收益以及開源社區貢獻。 #1. 資料中心面臨的問題 隨著滴滴 ......

    uj5u.com 2020-09-10 06:10:21 more
  • 滴滴資料通道服務演進之路

    **?桔妹導讀:**滴滴資料通道引擎承載著全公司的資料同步,為下游實時和離線場景提供了必不可少的源資料。隨著任務量的不斷增加,資料通道的整體架構也隨之發生改變。本文介紹了滴滴資料通道的發展歷程,遇到的問題以及今后的規劃。 #1. 背景 資料,對于任何一家互聯網公司來說都是非常重要的資產,公司的大資料 ......

    uj5u.com 2020-09-10 06:11:05 more
  • 滴滴AI Labs斬獲國際機器翻譯大賽中譯英方向世界第三

    **桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......

    uj5u.com 2020-09-10 06:11:29 more
  • MPP (Massively Parallel Processing)大規模并行處理

    1、什么是mpp? MPP (Massively Parallel Processing),即大規模并行處理,在資料庫非共享集群中,每個節點都有獨立的磁盤存盤系統和記憶體系統,業務資料根據資料庫模型和應用特點劃分到各個節點上,每臺資料節點通過專用網路或者商業通用網路互相連接,彼此協同計算,作為整體提供 ......

    uj5u.com 2020-09-10 06:11:41 more
  • 滴滴資料倉庫指標體系建設實踐

    **桔妹導讀:**指標體系是什么?如何使用OSM模型和AARRR模型搭建指標體系?如何統一流程、規范化、工具化管理指標體系?本文會對建設的方法論結合滴滴資料指標體系建設實踐進行解答分析。 #1. 什么是指標體系 ##1.1 指標體系定義 指標體系是將零散單點的具有相互聯系的指標,系統化的組織起來,通 ......

    uj5u.com 2020-09-10 06:12:52 more
  • 單表千萬行資料庫 LIKE 搜索優化手記

    我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......

    uj5u.com 2020-09-10 06:13:25 more
  • 滴滴Ceph分布式存盤系統優化之鎖優化

    **桔妹導讀:**Ceph是國際知名的開源分布式存盤系統,在工業界和學術界都有著重要的影響。Ceph的架構和演算法設計發表在國際系統領域頂級會議OSDI、SOSP、SC等上。Ceph社區得到Red Hat、SUSE、Intel等大公司的大力支持。Ceph是國際云計算領域應用最廣泛的開源分布式存盤系統, ......

    uj5u.com 2020-09-10 06:14:51 more
  • es~通過ElasticsearchTemplate進行聚合~嵌套聚合

    之前寫過《es~通過ElasticsearchTemplate進行聚合操作》的文章,這一次主要寫一個嵌套的聚合,例如先對sex集合,再對desc聚合,最后再對age求和,共三層嵌套。 Aggregations的部分特性類似于SQL語言中的group by,avg,sum等函式,Aggregation ......

    uj5u.com 2020-09-10 06:14:59 more
  • 爬蟲日志監控 -- Elastc Stack(ELK)部署

    傻瓜式部署,只需替換IP與用戶 導讀: 現ELK四大組件分別為:Elasticsearch(核心)、logstash(處理)、filebeat(采集)、kibana(可視化) 下載均在https://www.elastic.co/cn/downloads/下tar包,各組件版本最好一致,配合fdm會 ......

    uj5u.com 2020-09-10 06:15:05 more
最新发布
  • 選讀SQL經典實體筆記06_日期處理(上)

    ![](https://img2023.cnblogs.com/blog/3076680/202307/3076680-20230711160327907-1137777359.png) # 1. 計算一年有多少天 ## 1.1. 方案 ### 1.1.1. 找到當前年份的第一天 ### 1.1.2 ......

    uj5u.com 2023-07-13 08:35:14 more
  • 【技識訓累】Mysql中的SQL語言【技術篇】【二】

    博客推行版本更新,成果積累制度,已經寫過的博客還會再次更新,不斷地琢磨,高質量高數量都是要追求的,工匠精神是學習必不可少的精神。因此,大家有何建議歡迎在評論區踴躍發言,你們的支持是我最大的動力,你們敢投,我就敢肝 ......

    uj5u.com 2023-07-13 08:35:08 more
  • sharding-jdbc分庫連接數優化

    本文介紹了分庫分表的概念及優勢,以及sharding-jdbc分庫分表中間件,探究了sharding-jdbc的路由規則的執行流程 ......

    uj5u.com 2023-07-13 08:35:03 more
  • 掌握把“爛”SQL牢牢關進籠子里的密鑰

    摘要:本文通過5個部分內容幫助開發者快速了解GaussDB(DWS) 資源管理機制,讓數倉過載煩惱不再,把“爛”SQL牢牢關進籠子里。 本文分享自華為云社區《直播回顧 | 掌握把“爛”SQL牢牢關進籠子里的密鑰》,作者: 華為云社區精選 。 混合負載場景下,怎樣避免“爛”陳述句對資料庫系統的沖擊?如何 ......

    uj5u.com 2023-07-13 08:34:52 more
  • 再獲認可!萬里資料庫參編中國信通院資料庫研究報告 GreatSQL入選

    當前,全球數字經濟加速發展,資料正在成為重組全球要素資源、重塑全球經濟結構、改變全球競爭格局的關鍵力量。**資料庫作為存盤與處理資料的關鍵技術,在數字經濟浪潮下,不斷涌現新技術、新業態、新模式。** 7月4-5日,**由中國通信標準化協會和中國資訊通信研究院主辦**,大資料技術標準推進委員會承辦,I ......

    uj5u.com 2023-07-13 08:34:38 more
  • 掌握把“爛”SQL牢牢關進籠子里的密鑰

    摘要:本文通過5個部分內容幫助開發者快速了解GaussDB(DWS) 資源管理機制,讓數倉過載煩惱不再,把“爛”SQL牢牢關進籠子里。 本文分享自華為云社區《直播回顧 | 掌握把“爛”SQL牢牢關進籠子里的密鑰》,作者: 華為云社區精選 。 混合負載場景下,怎樣避免“爛”陳述句對資料庫系統的沖擊?如何 ......

    uj5u.com 2023-07-13 08:34:15 more
  • 選讀SQL經典實體筆記06_日期處理(上)

    ![](https://img2023.cnblogs.com/blog/3076680/202307/3076680-20230711160327907-1137777359.png) # 1. 計算一年有多少天 ## 1.1. 方案 ### 1.1.1. 找到當前年份的第一天 ### 1.1.2 ......

    uj5u.com 2023-07-13 08:33:42 more
  • 【技識訓累】Mysql中的SQL語言【技術篇】【二】

    博客推行版本更新,成果積累制度,已經寫過的博客還會再次更新,不斷地琢磨,高質量高數量都是要追求的,工匠精神是學習必不可少的精神。因此,大家有何建議歡迎在評論區踴躍發言,你們的支持是我最大的動力,你們敢投,我就敢肝 ......

    uj5u.com 2023-07-13 08:33:11 more
  • sharding-jdbc分庫連接數優化

    本文介紹了分庫分表的概念及優勢,以及sharding-jdbc分庫分表中間件,探究了sharding-jdbc的路由規則的執行流程 ......

    uj5u.com 2023-07-13 08:33:06 more
  • 再獲認可!萬里資料庫參編中國信通院資料庫研究報告 GreatSQL入選

    當前,全球數字經濟加速發展,資料正在成為重組全球要素資源、重塑全球經濟結構、改變全球競爭格局的關鍵力量。**資料庫作為存盤與處理資料的關鍵技術,在數字經濟浪潮下,不斷涌現新技術、新業態、新模式。** 7月4-5日,**由中國通信標準化協會和中國資訊通信研究院主辦**,大資料技術標準推進委員會承辦,I ......

    uj5u.com 2023-07-13 08:32:52 more