oracle - 抽出ソースに指定された抽出フィールドが含まれていません

okwaves2024-01-25  8

VARCHAR2 である StartDate から月と年を抽出しようとしています (これは非常に大きな間違いですが、私の失敗ではありません)。何を試しても常にエラーが発生します。問題が STARTDATE にあることがわかりました

次のようなクエリがあります:

SELECT SUM(DAYS) absenceDays
  FROM user_timesheets_absence
 WHERE UserID = 348
   AND EXTRACT(MONTH FROM to_date(StartDate)) = '2020-02-06'
   AND EXTRACT(YEAR FROM to_date(StartDate)) = '2020-02-06'
   AND Approved = 1

Stackoverflow でいくつかの投稿をチェックしましたが、何を試してもエラー メッセージが表示されます。

最初の投稿

2 番目の投稿

ご覧のとおり、TO_DATE フィールドを追加しましたが、常にエラーが発生します。

ORA-01861: literal does not match format string
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.

日付の形式も「YYYY-MM-DD」という表記法があります

ここで私が何を間違えたのか教えてくれる人はいますか?問題はどこから来ているのでしょうか? VARCHAR2 に問題がありますか?

値が「2020-02-06」のような文字列の場合、「2020-02-%」のような開始日をクエリできませんか?本当にすべてのデータ型変換が必要ですか?

– ウィリアム・ロバートソン

2020 年 9 月 3 日 12:16



------------------------

抽出では投稿した内容が返されません。

startdate が yyyy-mm-dd の場合は、適切な形式マスクを指定した to_date を使用して日付に変換します。抽出では、「文字列」全体ではなく、月または年が返されます。引用;:

SQL> with test (startdate) as
  2    (select '2020-09-03' from dual)
  3  select to_date(startdate, 'yyyy-mm-dd') col1,
  4    extract(month from (to_date(startdate, 'yyyy-mm-dd'))) col2,
  5    extract(year  from (to_date(startdate, 'yyyy-mm-dd'))) col3
  6  from test;

COL1           COL2       COL3
-------- ---------- ----------
03.09.20          9       2020

SQL>

あなたの場合:

and extract(month from to_date(startdate, 'yyyy-mm-dd')) = 6
and extract(year  from to_date(startdate, 'yyyy-mm-dd')) = 2020

0

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。