24

次の前菜を持つ varchar2 タイプのtransdate列があります

01/02/2012
01/03/2012

to_date 関数を使用して、別の列の日付形式に変換しました。これは私が得たフォーマットです。

01-JAN-2012
03-APR-2012

週番号を抽出しようとすると、すべての null 値が取得されます。

テーブル名から週番号として to_char(to_date(TRANSDATE), 'w') を選択します。

null
null

上記の形式で日付からweeknoを取得するには?

4

4 に答える 4

71

varchar2日付を真のdateデータ型に変換した後、varchar2目的のマスクを使用して元に変換します。

to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW')

データ型に週番号が必要な場合numberは、ステートメントをto_number()次のようにラップできます。

to_number(to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW'))

ただし、考慮すべき週番号オプションがいくつかあります。

WW  Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W   Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW  Week of year (1-52 or 1-53) based on the ISO standard.
于 2013-05-13T20:01:17.657 に答える
5

「w」を「iw」に置き換えてみてください。例えば:

SELECT to_char(to_date(TRANSDATE, 'dd-mm-yyyy'), 'iw') as weeknumber from YOUR_TABLE;
于 2013-05-13T19:59:44.400 に答える
0
Select last_name, round (sysdate-hire_date)/7,0) as tuner 
  from employees
  Where department_id = 90 
  order by last_name;
于 2015-10-04T08:30:58.357 に答える
-1

'dd-mon-yyyy'回答で指定された 2 番目の日付形式を使用している場合に使用します。元:

to_date(<column name>,'dd-mon-yyyy')
于 2015-01-28T12:27:52.093 に答える