Extract Num-Value from String

  • Hi,
    how can extract the num Value from this String?. Num-Value can have var length.

    Start: =
    End: )

    '#BAVDB#(RowCount=3204)'

    Result: 3204

    Regards
    Nicole


  • DECLARE @String VARCHAR(50) = '#BAVDB#(RowCount=3204)';

    -- As a single statement
    SELECT NumVal = SUBSTRING(@String, CHARINDEX('=', @String) +1, CHARINDEX(')', @String, CHARINDEX('=', @String) +1) - CHARINDEX('=', @String) -1);

    -- With a cte to identify the start and end positions
    WITH ctePos AS (
    SELECT
      StartPos = CHARINDEX('=', @String)
      , EndPos = CHARINDEX(')', @String, CHARINDEX('=', @String) +1)
    )
    SELECT NumVal = SUBSTRING(@String, StartPos +1, EndPos - StartPos -1)
    FROM ctePos;

  • Try this :

    DECLARE @String VARCHAR(50) = '#BAVDB#(RowCount=3204)';

    SELECT REPLACE(Right(@String,LEN(@String)-CHARINDEX('=',@String)),')','')

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply