create FUNCTION [dbo].[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 @S
END
调用select [dbo].[GET_NUMBER](age) as age from table ------解决方案--------------------
这个不错,楼主试试 ------解决方案--------------------
with tbl as
(
select '18' as a
union all
select '18岁' as a
union all
select '19岁零3个月' as a
union all
select '8岁' as a
union all
select '9' as a
)
select N'年龄' = case
when PatIndex('%[^0-9]%',a) = 0 then a
else stuff(a,PatIndex('%[^0-9]%',a),len(a),'') end
from tbl