假设我有以下几个字段:
abd738927
jaksm234234
hfk342
ndma0834
jon99322
输入:varchar。
如何仅显示此数值以显示:
738927
234234
342
0834
99322
尝试过子串,但数据的长度不一样,由于无法转换,任何想法都无法投射?
答案 0 :(得分:16)
以下是PATINDEX的示例:
select SUBSTRING(fieldName, PATINDEX('%[0-9]%', fieldName), LEN(fieldName))
这假定(1)字段将具有数字,(2)数字全部组合在一起,以及(3)数字后面没有任何后续字符。
答案 1 :(得分:4)
如果你不想创建一个函数,你可以这样:
cast(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(replace(replace(replace(YOUR_COLUMN
,'A',''),'B',''),'C',''),'D',''),'E',''),'F',''),'G',''),'H',''),'I',''),'J','')
,'K',''),'L',''),'M',''),'N',''),'O',''),'P',''),'Q',''),'R',''),'S',''),'T','')
,'U',''),'V',''),'W',''),'X',''),'Y',''),'Z',''),'$',''),',',''),' ','') as float)
答案 2 :(得分:3)
DECLARE @NonNumeric varchar(1000) = 'RGI000Testing1000'
DECLARE @Index int
SET @Index = 0
while 1=1
begin
set @Index = patindex('%[^0-9]%',@NonNumeric)
if @Index <> 0
begin
SET @NonNumeric = replace(@NonNumeric,substring(@NonNumeric,@Index, 1), '')
end
else
break;
end
select @NonNumeric -- 0001000
答案 3 :(得分:3)
仅提取数字(不使用while循环)并检查每个字符以查看它是否为数字并将其提取
Declare @s varchar(100),@result varchar(100)
set @s='as4khd0939sdf78'
set @result=''
select
@result=@result+
case when number like '[0-9]' then number else '' end from
(
select substring(@s,number,1) as number from
(
select number from master..spt_values
where type='p' and number between 1 and len(@s)
) as t
) as t
select @result as only_numbers
答案 4 :(得分:2)
我认为你想要VBA的Val()
功能。使用IsNumeric()
create function Val
(
@text nvarchar(40)
)
returns float
as begin
-- emulate vba's val() function
declare @result float
declare @tmp varchar(40)
set @tmp = @text
while isnumeric(@tmp) = 0 and len(@tmp)>0 begin
set @tmp=left(@tmp,len(@tmp)-1)
end
set @result = cast(@tmp as float)
return @result
end
答案 5 :(得分:2)
如果图像中有如上所示的数据,请使用以下查询
select field_3 from table where PATINDEX('%[ ~`!@#$%^&*_()=+\|{};",<>/?a-z]%', field_3)=0
结果将如下所示
答案 6 :(得分:1)
select substring(
'jaksm234234',
patindex('%[0-9]%','jaksm234234'),
LEN('jaksm234234')-patindex('%[0-9]%','jaksm234234')+2
)
答案 7 :(得分:0)
仅提取字符串中的数字。返回包含所有数字的字符串。示例:this1is2one345long6789number将返回123456789
CREATE FUNCTION [dbo].[GetOnlyNumbers] (@Temp VARCHAR(1000))
RETURNS VARCHAR (1000) AS BEGIN
DECLARE @KeepValues AS VARCHAR(50)
SET @KeepValues = '%[^0-9]%'
WHILE PATINDEX(@KeepValues, @Temp) > 0
SET @Temp = STUFF(@Temp, PATINDEX(@KeepValues, @Temp), 1, '')
RETURN @Temp
END