我不确定是否可以使用sql。我试图从字符串中提取所有数值。
declare @mytable table
(
myvalue varchar (50)
)
insert @mytable
select 'DOBIH3HA3' UNION ALL
select 'TAARE567ZAMEEN5' UNION ALL
select 'GAG645JAMU43'
以下方法非常封闭,但没有获得所需的输出。
SELECT LEFT(subsrt, PATINDEX('%[^0-9]%', subsrt + 't') - 1)
FROM (
SELECT subsrt = SUBSTRING(myvalue, pos, LEN(myvalue))
FROM (
SELECT myvalue, pos = PATINDEX('%[0-9]%', myvalue)
FROM @mytable
) d
) t
请分享您的专业知识....非常感谢
答案 0 :(得分:3)
Handles
答案 1 :(得分:2)
使用临时计数表和交叉应用
Select A.*
,B.*
From @mytable A
Cross Apply (
Select String=(Select Substring(A.myvalue,N,1)
From (Select Top (Len(A.myvalue)) N=Row_Number() Over (Order By Number) From master..spt_values ) NA
Where Substring(A.myvalue,N,1) Like '[0-9]'
For XML Path('') )
) B
返回
myvalue String
A2SK3HSDSK3 233
KGI6620GYUIG 6620
GAG4444BY9Y 44449
答案 2 :(得分:1)
首先我们需要创建function
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 > 0
BEGIN
SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
END
END
RETURN ISNULL(@strAlphaNumeric,0)
END
GO
create table #mytable
(
myvalue varchar (50)
)
insert #mytable
select 'A2SK3HSDSK3' UNION ALL
select 'KGI6620GYUIG' UNION ALL
select 'GAG4444BY9Y'
SELECT dbo.udf_GetNumeric(myvalue)
from #mytable
output
233
6620
44
449
答案 3 :(得分:1)
-- ================================================
-- Template generated from Template Explorer using:
-- Create Scalar Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
ALTER FUNCTION numonly
(
-- Add the parameters for the function here
@p nvarchar(max)
)
RETURNS nvarchar(max)
AS
BEGIN
-- Declare the return variable here
DECLARE @ResultVar nvarchar(max)
DECLARE @L int = LEN(@P)
WHILE @L > 0
BEGIN
SELECT @ResultVar = SUBSTRING(@P , @L , 1) + COALESCE(@ResultVar, '') WHERE SUBSTRING(@P , @L , 1) BETWEEN '0' AND '9';
SET @L = @L - 1;
END
-- Return the result of the function
RETURN @ResultVar
END
GO
select dbo.numonly(myvalue) from @mytable;