Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4625172
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:14:03+00:00 2026-05-22T03:14:03+00:00

I’m useing SQL Ser 2008 and have a large table with only one column

  • 0

I’m useing SQL Ser 2008 and have a large table with only one column of data. The data is a random string with very little consistency. Eample: Name Account 445566 0010020056893010445478008 AFD 369. I’ve been working with a split function that a stackoverflow user suggested. It works great but the function assigns the split string into one column. I need a row of individual columns. The present result is 1col with values Name, Account, 445566,… in it but the result I’m looking for is col1 Name, col2 Account, col3 445566,…
If anyone could provide some insight on how to tailor this script or its usage to get the desired result it would be much appreciated.

CREATE FUNCTION [dbo].[Split] 
(    
 @String varchar(max) 
,@Delimiter char 
) 
RETURNS @Results table 
( 
 Ordinal int 
,StringValue varchar(max) 
) 
as 
begin 

set @String = isnull(@String,'') 
set @Delimiter = isnull(@Delimiter,'') 

declare 
 @TempString varchar(max) = @String 
,@Ordinal int = 0 
,@CharIndex int = 0 

set @CharIndex = charindex(@Delimiter, @TempString) 
while @CharIndex != 0 begin      
    set @Ordinal += 1        
    insert @Results values 
    ( 
     @Ordinal 
    ,substring(@TempString, 0, @CharIndex) 
    )        
    set @TempString = substring(@TempString, @CharIndex + 1, len(@TempString) - @CharIndex)      
    set @CharIndex = charindex(@Delimiter, @TempString) 
end 

if @TempString != '' begin 
    set @Ordinal += 1  
    insert @Results values 
    ( 
     @Ordinal 
    ,@TempString 
    ) 
end 

return 
end 

--The usage:
SELECT    
* 
FROM    
mytable M    
CROSS APPLY    
[dbo].[Split] (M.TheColumn, ' ') S
Where rtrim(s.StringValue) != '' 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:14:04+00:00Added an answer on May 22, 2026 at 3:14 am

    If you know that you have 6 columns in the string you can use a split functions that looks like this and of course modify the function to whatever number of columns you want. A function can not return a dynamic number of columns.

    create function dbo.Split6(@String varchar(max), @Delimiter char(1)) 
    returns table as return
    (
      select
        substring(T.Col, 1, S1.Pos-1) as Col1,
        substring(T.Col, S1.Pos+1, S2.Pos-S1.Pos-1) as Col2,
        substring(T.Col, S2.Pos+1, S3.Pos-S2.Pos-1) as Col3,
        substring(T.Col, S3.Pos+1, S4.Pos-S3.Pos-1) as Col4,
        substring(T.Col, S4.Pos+1, S5.Pos-S4.Pos-1) as Col5,
        substring(T.Col, S5.Pos+1, S6.Pos-S5.Pos-1) as Col6
      from (select @String+replicate(@Delimiter, 6)) as T(Col)
        cross apply (select charindex(@Delimiter, T.Col, 1)) as S1(Pos)
        cross apply (select charindex(@Delimiter, T.Col, S1.Pos+1)) as S2(Pos)
        cross apply (select charindex(@Delimiter, T.Col, S2.Pos+1)) as S3(Pos)
        cross apply (select charindex(@Delimiter, T.Col, S3.Pos+1)) as S4(Pos)
        cross apply (select charindex(@Delimiter, T.Col, S4.Pos+1)) as S5(Pos)
        cross apply (select charindex(@Delimiter, T.Col, S5.Pos+1)) as S6(Pos)
    )
    

    Test:

    declare @T table (Col varchar(100))
    
    insert into @T values
     ('Name Account 445566 0010020056893010445478008 AFD 369'),
     (''),
     ('1 2'),
     ('1  3')
    
    select S.Col1, S.Col2, S.Col3, S.Col4, S.Col5, S.Col6
    from @T as T
      cross apply
        dbo.Split6(T.Col, ' ') as S
    

    Result:

    Col1  Col2     Col3    Col4                       Col5  Col6
    ----  -------  ------  -------------------------  ----  ----
    Name  Account  445566  0010020056893010445478008  AFD   369
    
    1     2             
    1              3            
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.