---
title: "SQL Server取字符串中数字"
author: "chenhsong"
date: "2014-01-02T14:56:10Z"
tags: []
categories: ["数据库"]
slug: "sql-server"
draft: false
lastmod: "2014-01-02T14:56:10Z"
id: 1998329
url: "https://omgdba.iteye.com/blog/1998329"
---

```
alter FUNCTION dbo.F_Get_Number (@S VARCHAR(100))
RETURNS VARCHAR(100)
AS 
BEGIN
    WHILE PATINDEX('%[^0-9]%',@S)<0
          BEGIN
                set @s=stuff(@s,patindex('%[^0-9]%',@s),1,'')
          END
    RETURN REPLACE(@S,'?','');
END
GO
```

   
![](http://dl2.iteye.com/upload/attachment/0092/7635/de8a98b2-19aa-3145-9c44-2969e83f6b0b.png)