Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string into individual characters in Sql Server 2005

Hi I have an input as

ID  data
1   hello
2   sql

The desired output being

ID  RowID  Chars
1    1     H
1    2     e
1    3     l
1    4     l
1    5     o
2    1     s
2    2     q
2    3     l

My approach so far being

Declare @t table(ID  INT IDENTITY , data varchar(max))
Insert into @t Select 'hello' union all select 'sql'
--Select * from @t
;With CteMaxlen As(
Select MaxLength = max(len(data)) from @t)
, Num_Cte AS
(     
      SELECT 1 AS rn
      UNION ALL
      SELECT rn +1 AS rn 
      FROM Num_Cte 
      WHERE rn <(select MaxLength from CteMaxlen)
)
-- Shred into individual characters
, Get_Individual_Chars_Cte AS
( 
      SELECT  
            ID
            ,Row_ID =ROW_NUMBER() Over(PARTITION by ID Order by ID)
            ,chars               
      FROM @t,Num_Cte
      CROSS APPLY( SELECT SUBSTRING((select data from  @t),rn,1)  AS chars) SplittedChars       
)

Select * from Get_Individual_Chars_Cte 

The query is not working at all with an exception being

Msg 512, Level 16, State 1, Line 4
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Edit :

I found my answer

;with Get_Individual_Chars_Cte AS
( 
   SELECT 
        ID,
        Row_ID =ROW_NUMBER() Over(PARTITION by ID Order by ID) 
        ,SUBSTRING(Data,Number,1) AS [Char]--,

FROM @t  
INNER JOIN master.dbo.spt_values ON
 Number BETWEEN 1 AND LEN(Data)
 AND type='P'

)

Select * from Get_Individual_Chars_Cte 

Help needed

like image 622
aditi Avatar asked Jun 16 '11 04:06

aditi


People also ask

How can I split a string into characters in SQL?

The STRING_SPLIT(string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT() function and ' ' as the second argument. FROM STRING_SPLIT( 'An example sentence.

How do I split a string with a comma in SQL Server?

Split comma-separated value string in a column. SELECT ProductId, Name, value FROM Product CROSS APPLY STRING_SPLIT(Tags, ','); Here is the result set. The order of the output may vary as the order is not guaranteed to match the order of the substrings in the input string.

How do I split a string with spaces in SQL Server 2012?

You can split the string using XML . You first need to convert the string to XML and replace the space with start and end XML tags . Once the string is converted into XML , you can use XQuery to get the result in proper format.


1 Answers

;with cte as
(
  select ID,
         substring(data, 1, 1) as Chars,
         stuff(data, 1, 1, '') as data,
         1 as RowID
  from @t
  union all
  select ID,
         substring(data, 1, 1) as Chars,
         stuff(data, 1, 1, '') as data,
         RowID + 1 as RowID
  from cte
  where len(data) > 0
)
select ID, RowID, Chars
from cte
order by ID, RowID
like image 143
Mikael Eriksson Avatar answered Oct 11 '22 12:10

Mikael Eriksson