본문 바로가기

DB/mssql

[function] 문자열에서 숫자만 뽑아내기



CREATE FUNCTION fn_GetNumeric (@strAlphaNumeric VARCHAR(256)) 

returns VARCHAR(256) 

AS 

  BEGIN 

      DECLARE @intAlpha INT 


      SET @intAlpha = Patindex('%[^0-9]%', @strAlphaNumeric) 


      BEGIN 

          WHILE @intAlpha > 0 

            BEGIN 

                SET @strAlphaNumeric = Stuff(@strAlphaNumeric, @intAlpha, 1, '') 

                SET @intAlpha = Patindex('%[^0-9]%', @strAlphaNumeric) 

            END 

      END 


      RETURN Isnull(@strAlphaNumeric, 0) 

  END




예)


SELECT dbo.fn_GetNumeric('1,200㎡')


result)

1200