0

I'm looking at pulling two numeric values from a single text string in SQL (2012 version i believe).

The strings are in the following possible formats:

A1234 B4567

or

A:1234 B:4567

or random variants of

A[Symbol/Space/Nothing][Numberstring1] [Space/Nothing] B[Symbol/Space/Nothing][Numberstring2] 

Ideally I'd use a combo of substring and charindex but because the symbols/space/nothing at all are used at random i'm finding it difficult. My preferred output would be 2 columns, one with the [NumberString1] one with [Numberstring2]

Any ideas folks?

4
  • Any value to reading all the values into memory and parsing in code? You can parse in SQL but any modern procedural language (Java, C#, JavaScript etc.) has much more powerful parsing support. This looks like a problem that Regular Expressions would be well suited to. Commented Jan 24, 2018 at 9:27
  • Ideally I'd push this out onto the .net developers to use a language suited for it but they're swamped and the responsibility has fallen on the Data Analysts. I could push back as it's currently done manually but I do like a puzzle so I'm poking away at it in SQL when I can. Commented Jan 24, 2018 at 9:48
  • These are just two fields delimited by <space>B right? Split them, remove A and B, remove any non numeric leading characters Commented Jan 24, 2018 at 10:22
  • Split was my first go to, unfortunately the space/delimiter isn't always present or the same Commented Jan 24, 2018 at 10:28

1 Answer 1

1

You could give this approach a try, it will give you a list of all the number groups. You could then select from the resulting table.

CREATE FUNCTION [dbo].[ParseOutNumbers](@inputText varchar(1000))
    RETURNS @ParsedValues TABLE (ID int IDENTITY(1,1),numVal varchar(1000))
AS
BEGIN
    DECLARE @charIndex INT
    DECLARE @number varchar(1000)
    DECLARE @nextChar varchar(1000)

    SELECT @charIndex = 1
    SELECT @number = ''

    WHILE @charIndex <= LEN(@inputText)
    BEGIN
        SELECT @nextChar = SUBSTRING(@inputText, @charIndex, 1);

        IF ISNUMERIC(@nextChar) = 1
        BEGIN
            SELECT @number = @number + @nextChar
        END

        IF (ISNUMERIC(@nextChar) = 0 OR @charIndex = LEN(@inputText))
        BEGIN
            IF (LEN(@number) > 0)
            BEGIN
                INSERT @ParsedValues(numVal) VALUES(@number)
                SELECT @number = ''
            END
        END

        SELECT @charIndex = @charIndex + 1
     END

RETURN
END

GO

select * from [dbo].[ParseOutNumbers]('A/d11222Bdd:22002    C23002')
select * from [dbo].[ParseOutNumbers]('A:11222 B:22002 C:23002')
select * from [dbo].[ParseOutNumbers]('A112442B22502C3002')

Alternate version:

CREATE FUNCTION [dbo].[ParseOutNumbersRev2](@inputText varchar(1000))
    RETURNS @ParsedValues TABLE (A varchar(1000), B varchar(1000))
AS
BEGIN
    DECLARE @charIndex INT
    DECLARE @number varchar(1000)
    DECLARE @nextChar varchar(1000)
    DECLARE @valueIndex INT
    SELECT @charIndex = 1
    SELECT @number = ''
    SELECT @valueIndex = 0
    WHILE @charIndex <= LEN(@inputText)
    BEGIN
        SELECT @nextChar = SUBSTRING(@inputText, @charIndex, 1);

        IF ISNUMERIC(@nextChar) = 1
        BEGIN
            SELECT @number = @number + @nextChar
        END

        IF (ISNUMERIC(@nextChar) = 0 OR @charIndex = LEN(@inputText))
        BEGIN
            IF (LEN(@number) > 0)
            BEGIN
                IF (@valueIndex = 0)
                BEGIN
                    INSERT @ParsedValues(A, B) VALUES(@number, null)
                END
                ELSE IF (@valueIndex = 1)
                BEGIN
                    UPDATE @ParsedValues SET B = @number
                END

                SELECT @number = ''
                SELECT @valueIndex = @valueIndex + 1
            END
        END

        SELECT @charIndex = @charIndex + 1
     END

RETURN
END

GO

select * from [dbo].[ParseOutNumbersRev2]('A/d11222Bdd:22002    C23002')
select * from [dbo].[ParseOutNumbersRev2]('A:11222 B:56 C:23002')
select * from [dbo].[ParseOutNumbersRev2]('A112442B22502C3002')
Sign up to request clarification or add additional context in comments.

1 Comment

Marvellous Thank You. I'll pass this to our DBA to create and give it a whirl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.