我有一个 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
如何以上述格式从日期获取星期数?
我有一个 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
如何以上述格式从日期获取星期数?
将您的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.
尝试将 'w' 替换为 'iw'。例如:
SELECT to_char(to_date(TRANSDATE, 'dd-mm-yyyy'), 'iw') as weeknumber from YOUR_TABLE;
Select last_name, round (sysdate-hire_date)/7,0) as tuner
from employees
Where department_id = 90
order by last_name;
'dd-mon-yyyy'
如果您使用答案中指定的第二个日期格式,请使用。前任:
to_date(<column name>,'dd-mon-yyyy')