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

  • SEARCH
  • Home
  • 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 3879442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:43:37+00:00 2026-05-19T22:43:37+00:00

I have a string like: @TempCol = sigma_x1,sigma_x2,…,sigma_xd,XX,YY,ZZ So how could I get a

  • 0

I have a string like:

@TempCol = sigma_x1,sigma_x2,…,sigma_xd,XX,YY,ZZ

So how could I get a specific part of that string, based on, lets say an index.
so

  • if index is 0, get sigma_x1
  • if index is 1, get sigma_x2
  • if index is 2, get sigma_x3
  • if index is d-1,get sigma_xd
  • if index is d, get XX,
  • if index is d+1,get YY
    and so on.

Previously Andriy M solved a similar issue, his code gets a substring based on a nomber but returns a substring the following way:

  • if @d is 1,get sigma_x1
  • if @d is 2,get sigma_x1,sigma_x2
  • if @d is 3,get sigma_x1,sigma_x2,sigma_x3
  • if @d is 4,get sigma_x1,sigma_x2,sigma_x3,sigma_x4
  • if @d is d,get sigma_x1,sigma_x2,sigma_x3,sigma_x4,…,sigma_xd (ALL THE STRING)

How to update this procedure to get specific element?

DECLARE @TempCol varchar(max), @d int, @p int, @Result varchar(max);
SET @TempCol = 'item1,item2,itemA,itemB,item#,item$';
SET @d = 3;

SET @p = 1;
WHILE @d > 0 AND @p > 0 BEGIN
  SET @p = CHARINDEX(',', @TempCol, @p);
  IF @p > 0 SET @p = @p + 1;
  SET @d = @d - 1;
END;

IF @p = 0
  SET @Result = @TempCol
ELSE
  SET @Result = SUBSTRING(@TempCol, 1, @p - 2);

SELECT @Result;
  • 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-19T22:43:38+00:00Added an answer on May 19, 2026 at 10:43 pm

    just try this. hope this will meet your needs.

    create a function GetIndex, which accepts string and delimiter to split the string

    CREATE FUNCTION dbo.GetIndex(@String varchar(8000), @Delimiter char(1))       
     returns @temptable TABLE (itemindex int identity(1,1), items varchar(8000))       
     as       
     begin       
         declare @idx int       
         declare @slice varchar(8000)       
    
         select @idx = 1       
             if len(@String)<1 or @String is null  return       
    
         while @idx!= 0       
         begin       
             set @idx = charindex(@Delimiter,@String)       
             if @idx!=0       
                 set @slice = left(@String,@idx - 1)       
             else       
                 set @slice = @String       
    
             if(len(@slice)>0)  
                 insert into @temptable(Items) values(@slice)       
    
             set @String = right(@String,len(@String) - @idx)       
             if len(@String) = 0 break       
         end   
     return       
     end 
    

    and you can query like,
    suppose you need 4th index then

    select * from dbo.GetIndex(@TempCol,',') where itemindex = 4
    

    to get an item of 4th index then

    select items from dbo.GetIndex(@TempCol,',') where itemindex = 4
    

    to get item to a variable

    select @Aux = items from dbo.GetIndex(@TempCol,',') where itemindex = 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's presume that I have string like '=&?/;#+%' to be a part of my
I have string like this: 1112222355. How can I get array like that [111,2222,3,55]
I have string like this: http://someurl.com?someparameter=dhws6sd6cvg and i would like to mach part from
I have string like this: http://someurl.com?test&lettersg and I would like to mach part from
I have string like that: string val = 555*324-000 now, I need to remove
Lets say I have string like this: String q = foo (one) bla (two)
I have string like &amp;auml;s&amp;aring; that needs to be converted like äså so how
I have string like /sdcard/filename.pdf Can any one get me the regex to get
If i have string like that: string s = xzy...; how to convert it
If I have string like combination.py , I want to get the following result

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.