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 7088927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:51:38+00:00 2026-05-28T07:51:38+00:00

I need to write stored procedure that gets string. Each char in string have

  • 0

I need to write stored procedure that gets string. Each char in string have to be converted to int type and the converted type have to be inserted to the table.

  • 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-28T07:51:39+00:00Added an answer on May 28, 2026 at 7:51 am

    The numbers table solution I posted over on this question will be your most efficient solution. Print bullet before each sentence + new line after each sentence SQL will patch code in once I’m home

    Edit

    The base unit of work is the inline table-valued function. You might have heard about TVF and how they suck in SQL Server but that relates to the multi-statement types. Inlines are fine as the optimizer can make sense of them and not make terrible plans.

    dbo.StringSplit returns a single column (varchar) table with the values split based on the supplied delimiter. You can cut down the lines of code required (derived tables L0 to L5) if you already have numbers table or a fast number generator in your data. I assume you don’t. The technique of using a numbers table to split data is not mine but I trust the SQL luminaries who have done the analysis.

    You asked for a proc so I have supplied dbo.StringSplitToInts to comply but all it’s doing is calling the TVF with the proper parameters. You can extract the select statement and cast into inline code or wherever you need it.

    -- This function splits a delimited string with good performance
    -- characteristics
    CREATE FUNCTION dbo.StringSplit
    (
        @input varchar(8000)
    ,   @delimiter char(1) = ','
    )
    RETURNS
        table
    RETURN
    -- L0 to L5 simulate a numbers table
    -- http://billfellows.blogspot.com/2009/11/fast-number-generator.html
    WITH L0 AS
    (
        SELECT
            0 AS C
        UNION ALL
        SELECT
            0
    )
    , L1 AS
    (
        SELECT
            0 AS c
        FROM
            L0 AS A
            CROSS JOIN L0 AS B
    )
    , L2 AS
    (
        SELECT
            0 AS c
        FROM
            L1 AS A
            CROSS JOIN L1 AS B
    )
    , L3 AS
    (
        SELECT
            0 AS c
        FROM
            L2 AS A
            CROSS JOIN L2 AS B
    )
    , L4 AS
    (
        SELECT
            0 AS c
        FROM
            L3 AS A
            CROSS JOIN L3 AS B
    )
    , L5 AS
    (
        SELECT
            0 AS c
        FROM
            L4 AS A
            CROSS JOIN L4 AS B
    )
    , NUMS AS
    (
        SELECT
            ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS number
        FROM
            L5
    )
    , SOURCE_DATA (ID, content) AS
    (
        -- This query simulates your input data
        -- This implementation could be simplified as our function
        -- only accepts 1 row of data but this could be applied to 
        -- any category of problem, not just a single line of input
        SELECT 1, @input 
    )
    , MAX_LENGTH AS
    (
        -- this query is rather important. The current NUMS query generates a 
        -- very large set of numbers but we only need 1 to maximum lenth of our
        -- source data. We can take advantage of a 2008 feature of letting
        -- TOP take a dynamic value
        SELECT TOP (SELECT MAX(LEN(SD.content)) AS max_length FROM SOURCE_DATA SD)
            N.number
        FROM
            NUMS N
    )
    , MULTI_LINES AS
    (
        -- This query will make many lines out a single line based on the supplied delimiter
        -- Need to retain the ID (or some unique value from original data to regroup it
        -- http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum
        SELECT 
            SD.ID
        ,   LTRIM(substring(SD.content, Number, charindex(@delimiter, SD.content + @delimiter, Number) - Number)) AS lines
        FROM
            MAX_LENGTH
            CROSS APPLY
                SOURCE_DATA SD
        WHERE
            Number <= len(SD.content)
            AND substring(@delimiter + SD.content, Number, 1) = @delimiter
    )
    SELECT
        ML.lines
    FROM
        MULTI_LINES ML
    GO
    
    -- This is overkill as the function is more versatile but
    -- in the spirit of delivering what was asked for, this proc
    -- calls the function and casts the data to the appropriate type
    CREATE PROCEDURE dbo.StringSplitToInts
    (
        @input varchar(8000)
    ,   @delimiter char(1) = ','
    )
    AS
    BEGIN
        SET NOCOUNT ON
        SELECT
            CAST(SS.lines AS int) AS int_tokens
        FROM
            dbo.StringSplit(@input, @delimiter) SS
    
    END
    GO
    
    -- Over 9000!
    EXECUTE dbo.StringSplitToInts '100,200,300,500,9000'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a stored procedure that will search a table based on
I need to write a stored procedure that will provide the data from two
i need to write a stored procedure which will return a string.logic is when
In Oracle PL/SQL I need to write a stored procedure that may throw a
I want to write a stored procedure that queries XML files after I have
I need to write a Stored procedure in SQL server whose data returned will
I'm need to write a stored procedure for SQL Server 2008 for performing a
I have a stored procedure that uses this select statement: SELECT dbo.VendorProgram.id, dbo.VendorProgram.CODE, dbo.Programs.ProgramName
I have a date in the string format in a table and I need
Whenever I write a stored procedure for selecting data based on string variable (varchar,

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.