title: T-SQL to pull decimal values from a string
body: <p>Ok so what I am trying to do is pull the decimal values from a string. My issue is that the strings are not uniform. some may be 6.9% or 5.2mg/L and some may have no number values at all. What I would like to do is return just the decimal(or integer) value from the string and if that does not exist then return NULL.</p>
  
  <p>I have tried this function:</p>
  
  <pre><code>CREATE FUNCTION dbo.udf_GetNumeric
  (@strAlphaNumeric VARCHAR(256))
  RETURNS VARCHAR(256)
  AS
  BEGIN
  DECLARE @intAlpha INT
  SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
  BEGIN
  WHILE @intAlpha &gt; 0
  BEGIN
  SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
  SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
  END
  END
  RETURN ISNULL(@strAlphaNumeric,0)
  END
  </code></pre>
  
  <p>But that only returns the numbers with no decimal place.</p>