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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:00:12+00:00 2026-05-31T12:00:12+00:00

i was searching script to extract number from text in sql server and i

  • 0

i was searching script to extract number from text in sql server and i found this

CREATE FUNCTION [dbo].[GetNumbersFromText](@String VARCHAR(2000))
RETURNS @Number TABLE (Number INT)
AS
BEGIN
DECLARE @Count INT
DECLARE @IntNumbers VARCHAR(1000)
SET @Count = 0
SET @IntNumbers = ''
WHILE @Count <= LEN(@String)
BEGIN
--Find a numeric charactor
IF SUBSTRING(@String,@Count,1) >= '0' AND SUBSTRING(@String,@Count,1) <= '9'
BEGIN
SET @IntNumbers = @IntNumbers + SUBSTRING(@String,@Count,1)
END
--If the next charactor is not a numeric one, the current number ends, so add a    separator
IF (SUBSTRING(@String,@Count+1,1) < '0'OR SUBSTRING(@String,@Count+1,1) > '9') AND    SUBSTRING(@String,@Count,1) >= '0' AND SUBSTRING(@String,@Count,1) <= '9'
BEGIN
SET @IntNumbers = @IntNumbers + ','
END
SET @Count = @Count + 1
END
---Split string to give a table with the numbers in the text
INSERT INTO @Number
SELECT DISTINCT items FROM dbo.Split(@IntNumbers, ',')
return
END

and call it like

SELECT Number FROM Dbo.[GetNumbersFromText]('Give me 120 this week and 50 next week')

it works fine but i need more short code. can i use patindex to extract number from text.
please anyone share small & good logic to do so. thanks

  • 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-31T12:00:14+00:00Added an answer on May 31, 2026 at 12:00 pm

    This is a bit shorter. Turned it into Inline Table Function that uses a recursive CTE to find the numbers.

    create function [dbo].[GetNumbersFromText](@String varchar(2000))
    returns table as return
    (
      with C as
      (
        select cast(substring(S.Value, S1.Pos, S2.L) as int) as Number,
               stuff(s.Value, 1, S1.Pos + S2.L, '') as Value
        from (select @String+' ') as S(Value)
          cross apply (select patindex('%[0-9]%', S.Value)) as S1(Pos)
          cross apply (select patindex('%[^0-9]%', stuff(S.Value, 1, S1.Pos, ''))) as S2(L)
        union all
        select cast(substring(S.Value, S1.Pos, S2.L) as int),
               stuff(S.Value, 1, S1.Pos + S2.L, '')
        from C as S
          cross apply (select patindex('%[0-9]%', S.Value)) as S1(Pos)
          cross apply (select patindex('%[^0-9]%', stuff(S.Value, 1, S1.Pos, ''))) as S2(L)
        where patindex('%[0-9]%', S.Value) > 0
      )
      select Number
      from C
    )
    

    If you expect to have more than 100 numbers in the string you need to call it with option (maxrecursion 0).

    declare @S varchar(max)
    set @S = 'Give me 120 this week and 50 next week'
    select number from GetNumbersFromText(@S) option (maxrecursion 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello please me out regarding this function . Its searching script. when i am
I got this script while searching through stackoverflow string commonTextBoxPrefix = txt; foreach (Control
Searching here I found that this question was already asked , but I think
I have written (tried to) this small bash script for searching through a range
I have this Perl snippet from a script that I am translating into Python.
really struggling with this one. I've recently setup a bash shell script to extract,
I've been searching for this script all day. does anyone know any such script
Ok, so I've got this AutoHotkey snippet: ; Google text from any app ;
i got a script after searching google for Edit Image FaceBook Style but found
I'm searching for a jQuery or simple, old-school JavaScript script which makes text input

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.