[英]How Oracle SQL to get first day of month
要求是輸入日期應該產生當月的第一天。 條件是:
Ex: if i select Month OCT and Year 2021 then Startinvoice same with startbilldate and endinvoice get lastday of month from startinvoice but when i select Month NOV and Year 2021 then Startinvoice = 01 nov 2021 and endinvoice = 30 nov 2021 next month it should return 01st對應月份的日期。 在此處輸入圖像描述
您可以在這里使用CASE
表達式:
SELECT
dt,
CASE WHEN EXTRACT(day FROM dt) <= 15
THEN TRUNC(dt, 'MM')
ELSE TRUNC(ADD_MONTHS(dt, 1), 'MM') END AS dt_out
FROM yourTable;
這些條件基本上是平等的。 無論如何,11 月有 30 天,所以您的第一個條件包含在第二個條件中。 完全沒有區別。
因此,您只需將 date 值截斷為month ,例如
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate,
2 trunc(sysdate, 'mm') first_of_month
3 from dual;
SYSDATE FIRST_OF_MONTH
------------------- -------------------
21.12.2021 09:01:22 01.12.2021 00:00:00
SQL>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.