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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:30:23+00:00 2026-05-31T16:30:23+00:00

I have strings like the ones below in a SQL column. I want to

  • 0

I have strings like the ones below in a SQL column. I want to extract them as a Gigabyte amount in aggregate. Example:

Original Column ---------> Expected Output from a TSQL function
-------------------------------------------
$15 / 1GB 24m + Intern 120MB ----------> 1.12 GB
$19.95 / 500MB + $49.95 / 9GB Blackberry -----> 9.5GB
$174.95 Blackberry 24GB + $10 / 1GB Datapack ----> 25GB
$79 / 6GB --> 6GB
Null --> Null
$20 Plan --> 0GB

Note: for our purpose, 1000MB = 1 GB (not 1024).

The pattern is numbers followed by GB/MB, usually they are combined like 1GB (without any space but may sometimes may contain a space, it is not particularly important if hard to implement for this exception).

Sometimes there are up to three or four instances of GB/MB occurring in the same string which are usually separated by a + sign (see row 2 and 3 of my example above).

I have seen how we extract the dollar values in one of the answers where numbers were followed by $ or extract all integers in a string but I don’t want to extract the dollar values or all the integers in a string. I just want the sum of GB/MB in the string.

  • 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-31T16:30:24+00:00Added an answer on May 31, 2026 at 4:30 pm

    The following may appear somewhat specific and too assuming, even though it might also look a bit too complicated for a specific and over-assuming solution. Still, I hope it will at least make a good starting point.

    These are the assumptions I had to make to avoid complicating the script even further:

    1. The values to be extracted never contain a decimal point (are integers).

    2. The values to be extracted are always either preceded by a space or at the beginning of the column value.

    3. Neither GB nor MB can possibly be part of anything else than a traffic size (a value to be extracted).

    4. Neither GB nor MB is ever preceded by a space.

    5. All the strings are either unique or accompanied by another column or columns that can be used as key values. (My solution, in particular, uses an additional column as a key.)

    So, here’s my attempt (which did return the expected results for all the sample data provided in the original post):

    WITH data (id, str) AS (
                 SELECT 1, '$15 / 1GB 24m + Intern 120MB' ----------> 1.12 GB
      UNION ALL  SELECT 2, '$19.95 / 500MB + $49.95 / 9GB Blackberry' -----> 9.5GB
      UNION ALL  SELECT 3, '$174.95 Blackberry 24GB + $10 / 1GB Datapack' ----> 25GB
      UNION ALL  SELECT 4, '$79 / 6GB' --> 6GB
      UNION ALL  SELECT 5, Null --> Null
      UNION ALL  SELECT 6, '$20 Plan' --> 0GB
      UNION ALL  SELECT 7, '460MB' --> 0.46GB
    ),
    unified AS (
      SELECT
        id,
        oldstr = str,
        str = REPLACE(str, 'GB', '000MB')
      FROM data
    ),
    split AS (
      SELECT
        id,
        ofs    = 0,
        endpos = CHARINDEX('MB', str),
        length = ISNULL(CHARINDEX(' ', REVERSE(SUBSTRING(str, 1, NULLIF(CHARINDEX('MB', str), 0) - 1)) + ' ') - 1, 0),
        str    = SUBSTRING(str, NULLIF(CHARINDEX('MB', str), 0) + 2, 999999)
      FROM unified
      UNION ALL
      SELECT
        id,
        ofs    = NULLIF(endpos, 0) + 1,
        endpos = CHARINDEX('MB', str),
        length = ISNULL(CHARINDEX(' ', REVERSE(SUBSTRING(str, 1, NULLIF(CHARINDEX('MB', str), 0) - 1)) + ' ') - 1, 0),
        str    = SUBSTRING(str, NULLIF(CHARINDEX('MB', str), 0) + 2, 999999)
      FROM split
      WHERE length > 0
    ),
    extracted AS (
      SELECT
        d.id,
        str = d.oldstr,
        mb = CAST(SUBSTRING(d.str, s.ofs + s.endpos - s.length, s.length) AS int)
      FROM unified d
      INNER JOIN split s ON d.id = s.id
    )
    SELECT
      id,
      str,
      gb = RTRIM(CAST(SUM(mb) AS float) / 1000) + 'GB'
    FROM extracted
    GROUP BY id, str
    ORDER BY id
    

    Basically, the idea is first to convert all gigabytes to megabytes, to then be able search and extract only megabyte amounts. The search & extract method involves a recursive CTE and consists essentially of these steps:

    1) find the position of the first MB;

    2) find the length of the number immediately before the MB;

    3) cut off the beginning of the string right at the end of the first MB;

    4) repeat from Step 1 until no MB is found;

    5) join the found figures to the original string list to extract the amounts themselves.

    Afterwards, it only remains for us to group by key values and sum the obtained amounts. Here’s the output:

    id  str                                           gb
    --  --------------------------------------------  ------
    1   $15 / 1GB 24m + Intern 120MB                  1.12GB
    2   $19.95 / 500MB + $49.95 / 9GB Blackberry      9.5GB
    3   $174.95 Blackberry 24GB + $10 / 1GB Datapack  25GB
    4   $79 / 6GB                                     6GB
    5   NULL                                          NULL
    6   $20 Plan                                      0GB
    7   460MB                                         0.46GB
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have strings like 84, 03 etc. that I want to convert to Date
I have strings like: Avery® Laser & Inkjet Self-Adhesive I need to convert them
I have PHP array of arrays as below and I want to extract the
I have strings like this: http://localhost:2055/web-site-2009/paginas/noticias/**IGP-M recua 0,36% em agosto, aponta FGV**-46.aspx I'd like
I have strings like this: Car is blue String could also be like this:
I have strings like follow field_zip_code:48103 taxonomy:88 field_zip_code:48103 taxonomy:88 field_state:MI field_zip_code:48103 From here i
If you have strings like: file_0 file_1 file_2 file_3 file_4 file_5 file_6 file_11 how
Suppose I have strings like the following : OneTwo ThreeFour AnotherString DVDPlayer CDPlayer I
Let's say I have strings like this: PH&N 2015 LifeTime Series D PH&N 2020
I have many strings like 20120117 and 20120321. I need to convert it in

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.