use*_*404 18 sql oracle oracle-sqldeveloper
我有一个varchar2类型的transdate列,它有以下主菜
01/02/2012
01/03/2012
Run Code Online (Sandbox Code Playgroud)
等等
我使用to_date函数将其转换为另一列中的日期格式.这是我得到的格式.
01-JAN-2012
03-APR-2012
Run Code Online (Sandbox Code Playgroud)
当我试图提取weekno时,我得到所有空值.
选择to_char(to_date(TRANSDATE),'w')作为tablename的weekno.
null
null
Run Code Online (Sandbox Code Playgroud)
如何以上述格式从日期获取weekno?
Wol*_*olf 58
将varchar2
日期转换为true date
数据类型后,转换回varchar2
所需的掩码:
to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW')
Run Code Online (Sandbox Code Playgroud)
如果您想要number
数据类型中的周数,可以将语句包装在to_number()
:
to_number(to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW'))
Run Code Online (Sandbox Code Playgroud)
但是,您需要考虑几周的数字选项:
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.
Run Code Online (Sandbox Code Playgroud)
尝试将 'w' 替换为 'iw'。例如:
SELECT to_char(to_date(TRANSDATE, 'dd-mm-yyyy'), 'iw') as weeknumber from YOUR_TABLE;
Run Code Online (Sandbox Code Playgroud)