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

来源:https://www.cnblogs.com/lying7/archive/2023/07/13/17545015.html
-Advertisement-
Play Games

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


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

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一 .準備一個空的Maven項目。 二. 配置pom文件,引入相關依賴。 <!--版本建議換成提示的更安全的版本--> <!-- mybatis插件 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifa ...
  • **本文深入探索了Django中的Cookie和Session,解析瞭如何應對HTTP協議的無狀態性問題,說明其基礎概念,分析工作原理,並討論何時應選擇使用Cookie或Session。文章進階部分,提出高效管理Cookie和Session,以及如何利用它們進行用戶身份驗證。** ## HTTP協議 ...
  • # 一、前言 最近在做一個項目,有個比較耗時的操作是啟用線程進行非同步操作,當時在啟用的線程時,突然發現子線程無法獲取父線程中的HttpServletRequest請求對象,因為是第一次遇到這種問題,所以記錄一下解決方案。 # 二、問題模擬 在這裡,我們簡單模擬一下出現的問題。我們首先編寫一個簡單的h ...
  • 在C#中調用StringBoot介面,POST請求,案例代碼: public string HttpPost() { //把用戶傳過來的數據轉成“UTF-8”的位元組流Encoding encoding = Encoding.UTF8;//創建一個HTTP請求HttpWebRequest reques ...
  • fdisk 命令 創建分區 實現擴容 Linux fdisk命令簡介 Linux fdisk 是一個創建和維護分區表的程式,它相容 DOS 類型的分區表、BSD 或者 SUN 類型的磁碟列表。 菜單操作說明 m :顯示菜單和幫助信息 a :活動分區標記/引導分區 d :刪除分區 l :顯示分區類型 ...
  • 作者:Bright-Ho 聯繫方式:[email protected] input輸入子系統框架分析(純軟體方面): 上一節,我們簡單的描述的什麼是輸入子系統;什麼是字元設備;以及其作用;重點是我們講到分析輸入子系統必須結合硬體設備來分析;那麼這一節,我們主要講解輸入子系統的軟體框架;接下來,我們就進 ...
  • 作者:Bright-Ho 聯繫方式:[email protected] 前言背景描述: 雖然在網上看了很多有關輸入子系統的資料和視頻,但是真正的,系統的,全面的,來弄清輸入子系統,還是要花些時間和精力的!現在我以一個初學者的角度來分析input輸入子系統; 那麼分析input輸入子系統之前,得先弄清楚 ...
  • # FRP使用方法 # 流程圖如下 >建議查看流程圖哦 ![image](https://img2023.cnblogs.com/blog/2368840/202307/2368840-20230712150836502-1824122109.png) ## 訪問FRP官方項目 ### [https ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...