Question

I'm using SQL Server and I would like to create a column (Temp) that uses part of another String (Description). I want to retrieve the GB information from Description:

The description field can look like: 'blah blah blah 100GB blah' - I'm interested in the '100GB'.

The problem is that the amount of GB and placement in the string can vary.

No correct solution

OTHER TIPS

I would recommend you to use regular expressions for that, but then you would also need to use CLR functions. However, you could specify the exact regular expression you want to use so it would be more accurate.

http://www.yumeidearmas.com/2013/10/02/regular-expressions-in-sql/

If you want an "easier" solution, maybe you could also use PATINDEX to find the index of the "GB". will be 100 the only number on the string (or you could also have some more?)?

declare @string nvarchar(100) = 'blah blah blah 100GB blah'

select SUBSTRING(@string,  PATINDEX('%[0-9]%', @string), PATINDEX('%GB%', @string)-PATINDEX('%[0-9]%', @string))

This case will work for your example, but if the format is different for some reason (there is no number, or there are 2 numbers, etc.) will not work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top