Here are two more solutions :-)
the logic is the same. But once I saw my solution in practice, i realized that I dont need to use SUM since we just need to chose MAX. Next since the CHAR 1 is more than the char 0 (char and not number) in most collate (cultures) then we don't need any CONVERT as well and we can just select the MAX from the CHAR. so here are the two solutions:
-- This solution fit all
;With MyCTE as (
select
SUBSTRING([timeSchedule],1,1) as c1,
SUBSTRING([timeSchedule],2,1) as c2,
SUBSTRING([timeSchedule],3,1) as c3,
SUBSTRING([timeSchedule],4,1) as c4,
SUBSTRING([timeSchedule],5,1) as c5,
SUBSTRING([timeSchedule],6,1) as c6,
SUBSTRING([timeSchedule],7,1) as c7,
SUBSTRING([timeSchedule],8,1) as c8,
SUBSTRING([timeSchedule],9,1) as c9
from myTable
)
select
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c1)))+
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c2)))+
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c3)))+
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c4)))+
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c5)))+
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c6)))+
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c7)))+
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c8)))+
CONVERT( NVARCHAR(50),MAX(CONVERT(INT,c9)))
from MyCTE
-- MAX char depends on collate (like sorting, comparing)
-- but this solution fit most collate as least, if not all,
-- since "1" bigger than "0"
-- In this solution you need to remember that you will not get the "zero padding"
-- the solution will be in the len of the bigger len
;With MyCTE as (
select
SUBSTRING([timeSchedule],1,1) as c1,
SUBSTRING([timeSchedule],2,1) as c2,
SUBSTRING([timeSchedule],3,1) as c3,
SUBSTRING([timeSchedule],4,1) as c4,
SUBSTRING([timeSchedule],5,1) as c5,
SUBSTRING([timeSchedule],6,1) as c6,
SUBSTRING([timeSchedule],7,1) as c7,
SUBSTRING([timeSchedule],8,1) as c8,
SUBSTRING([timeSchedule],9,1) as c9
from myTable
)
select
MAX(c1)+
MAX(c2)+
MAX(c3)+
MAX(c4)+
MAX(c5)+
MAX(c6)+
MAX(c7)+
MAX(c8)+
MAX(c9)
from MyCTE