Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, rajeswar.

Asked: January 18, 2017 - 3:39 am UTC

Last updated: January 19, 2017 - 12:58 am UTC

Version: 11gr2

Viewed 1000+ times

You Asked

hi sir,


My project


employee table one column values are(w,w,w,w,w,nw,nw) here(w means working , nw means notworking)

then result column value are (Su/M/Tu/W/Th9-17)

Su -- sunday
M -- Monday
.
.
.
.

convert column values into that format pls help me sir


thanks sir,

and Connor said...


SQL> create table t as
  2  select 'w,w,w,w,w,nw,nw' tag from dual
  3  union all
  4  select 'w,w,nw,nw,w,nw,nw' tag from dual
  5  union all
  6  select 'nw,w,nw,w,w,nw,w' tag from dual;

Table created.

SQL>
SQL>
SQL> with comma_pos as
  2  (
  3  select
  4    tag,
  5    instr(tag,',',1,1) i1,
  6    instr(tag,',',1,2) i2,
  7    instr(tag,',',1,3) i3,
  8    instr(tag,',',1,4) i4,
  9    instr(tag,',',1,5) i5,
 10    instr(tag,',',1,6) i6
 11  from t
 12  ),
 13  day_format as
 14  (
 15  select tag,
 16    rtrim(case when substr(tag,1,i1) = 'w,' then 'Su/'  end||
 17    case when substr(tag,i1,i2-i1) = ',w' then 'M/'   end||
 18    case when substr(tag,i2,i3-i2) = ',w' then 'Tu/'   end||
 19    case when substr(tag,i3,i4-i3) = ',w' then 'W/'   end||
 20    case when substr(tag,i4,i5-i4) = ',w' then 'Th/'   end||
 21    case when substr(tag,i5,i6-i5) = ',w' then 'F/'   end||
 22    case when substr(tag,i6) = ',w' then 'Sa'   end,'/') day_tag
 23  from  comma_pos
 24  )
 25  select tag, day_tag from day_format;

TAG               DAY_TAG
----------------- -----------------
w,w,w,w,w,nw,nw   Su/M/Tu/W/Th
w,w,nw,nw,w,nw,nw Su/M/Th
nw,w,nw,w,w,nw,w  M/W/Th/Sa

3 rows selected.



Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

rajeswar reddy, January 18, 2017 - 4:22 am UTC

good

thanks

rajeswar reddy, January 18, 2017 - 4:25 am UTC

thanks you very much sir
thanks alot
Connor McDonald
January 19, 2017 - 12:58 am UTC

glad we could help