안녕하세요. 고수님들께 질문이 있습니다.
최소 8자리에서 최대 30자리의 값이 들어가는 가변길이의 문자열 필드가 있습니다.
어떤 값은 8자리가, 어떤 값은 13자리, 24자리 등 불규칙한 문자열이 들어가는 필드인데
8자리마다 하이픈을 넣으려고 합니다.
예를들어
52774098 => 52774098
(8) (8)
A1B2C3D4E5 => A1B2C3D4-E5
(10) (8)-(2)
ZZXXRQQEEYY1234 => ZZXXRQQE-EYY1234
(15) (8)-(7)
201912310100311077778530 => 20191231-01003110-77778530
(24) (8)-(8)-(8)
어떻게 해야 할까요??
Comment 2
-
건우아빠
2019.12.05 17:21
with res as(select '52774098' txt union allselect 'A1B2C3D4E5' txt union allselect '201912310100311077778530' txt union allselect '20191ghgh77785304' txt union allselect '20191fghfghfgh107777853055565' txt union allselect '20191231j107jffj30775757575' txt ) ,result as(select * , SUBSTRING( txt , 8 * ( b.num - 1 ) + 1 ,8) btxt , CEILING(len(a.txt) / 8. ) sno , len(a.txt) lentxtfrom res across apply(select number + 1 numfrom master..spt_valueswhere type = 'P'and number < CEILING(len(a.txt) / 8. ) ) b)select txt, substring (isnull( max ( CASE num WHEN 1 THEN btxt END ) + '-' ,'') +isnull( max ( CASE num WHEN 2 THEN btxt END ) + '-' ,'') +isnull( max ( CASE num WHEN 3 THEN btxt END ) + '-' ,'') +isnull( max ( CASE num WHEN 4 THEN btxt END ) + '-' ,'') ,1,lentxt + sno -1)from resultGROUP BY txt ,lentxt ,sno -
안돼요
2019.12.11 12:21
와우..쉽지 않네요.. 답변 감사합니다^^