---- 20자 미만일때.. with res as ( select '가나다라 마바사 asd파타하' txt union all select '가나다라 마바' ) select a.txt , substring( a.txt , 10,1) , case ceiling( len(a.txt) / 10.) when 1 then a.txt when 2 then STUFF ( a.txt , 10 , 1 , substring( a.txt , 10,1)+'/' ) end from res a
-- 20자 이상이 있을때 -- http://www.sqler.com/141578 with res as ( select 1 id, '가나다라 마바사 asd파타하' txt union all select 2 id, '가나다라 ' txt union all select 3 id, '가나다라 마바사 asd파타하abcdefghijklmnopqrstuvwxyz' txt ) , result as ( select a.id , a.txt , b.no , substring( a.txt , ( b.no * 10) + 1, 10) name from res a cross apply ( select number [no] from master.dbo.spt_values where type = 'P' and number < ceiling( len(a.txt) / 10.) ) as b ) SELECT DISTINCT id , STUFF( (SELECT '/' + name AS [text()] FROM result b WHERE b.id = a.id FOR XML PATH('')) , 1, 1, '' ) AS name_csv FROM result a ORDER BY 1
---- 20자 미만일때..
with res as (
select '가나다라 마바사 asd파타하' txt union all
select '가나다라 마바'
)
select a.txt , substring( a.txt , 10,1)
, case ceiling( len(a.txt) / 10.)
when 1 then a.txt
when 2 then STUFF ( a.txt , 10 , 1 , substring( a.txt , 10,1)+'/' )
end
from res a
-- 20자 이상이 있을때
-- http://www.sqler.com/141578
with res as (
select 1 id, '가나다라 마바사 asd파타하' txt union all
select 2 id, '가나다라 ' txt union all
select 3 id, '가나다라 마바사 asd파타하abcdefghijklmnopqrstuvwxyz' txt
) ,
result as (
select a.id , a.txt , b.no
, substring( a.txt , ( b.no * 10) + 1, 10) name
from res a
cross apply
( select number [no]
from master.dbo.spt_values
where type = 'P'
and number < ceiling( len(a.txt) / 10.) ) as b )
SELECT
DISTINCT
id
, STUFF(
(SELECT '/' + name AS [text()] FROM result b WHERE b.id = a.id FOR XML PATH(''))
, 1, 1, ''
) AS name_csv
FROM result a
ORDER BY 1