24

我有一个 varchar2 类型的 transdate 列,其中包含以下主菜

01/02/2012
01/03/2012

等等

我使用 to_date 函数将其转换为另一列中的日期格式。这是我得到的格式。

01-JAN-2012
03-APR-2012

当我试图提取 weekno 时,我得到了所有空值。

从表名中选择 to_char(to_date(TRANSDATE), 'w') 作为 weekno。

null
null

如何以上述格式从日期获取星期数?

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'如果您使用答案中指定的第二个日期格式,请使用。前任:

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