How to extract week number in sql

后端 未结 4 1347
难免孤独
难免孤独 2020-11-29 05:40

I have a transdate column of varchar2 type which has the following entrees

01/02/2012
01/03/2012

etc.

I converted it in to date fo

4条回答
  •  再見小時候
    2020-11-29 06:38

    After converting your varchar2 date to a true date datatype, then convert back to varchar2 with the desired mask:

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

    If you want the week number in a number datatype, you can wrap the statement in to_number():

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

    However, you have several week number options to consider:

    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.
    

提交回复
热议问题