34,590
社区成员
发帖
与我相关
我的任务
分享
select case when isnull(durg_spec,'') =''
then null
else cast(substring(durg_spec,1,patindex('%[''a-z]%',durg_spec)-1) as float)
end as durg_spec
from tb
select case when isnull(durg_spec,'') =''
then null
else cast(substring(durg_spec,1,patindex('%[a-z]%',durg_spec)-1) as float)
end as durg_spec
from tb
;with tb(durg_spec)
as
(
select '0.25ug' union all
select '0.25ug' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '2.5mg' union all
select '2.5mg' union all
select '2.5mg' union all
select '2.5mg' union all
select '0.15g' union all
select '0.15g' union all
select '0.3g' union all
select '0.3g' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '5mg'
)
--方法3
select cast(stuff(durg_spec,
patindex('%[a-z]%',durg_spec),
len(durg_spec)-patindex('%[a-z]%',durg_spec)+1,
'') as float) as durg_spec
from tb
/*
durg_spec
0.25
0.25
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
2.5
2.5
2.5
2.5
0.15
0.15
0.3
0.3
0.2
0.2
0.2
0.2
0.2
0.2
5
*/
create table #tb(durg_spec nvarchar(50))
insert into #tb
select '0.25ug'
union all select '0.25ug'
union all select '0.5g'
union all select '0.5g'
union all select '0.5g'
union all select '0.5g'
union all select '0.5g'
union all select '0.5g'
union all select '0.5g'
union all select '0.5g'
union all select '0.5g'
union all select '2.5mg'
union all select '2.5mg'
union all select '2.5mg'
union all select '2.5mg'
union all select '0.15g'
union all select '0.15g'
union all select '0.3g'
union all select '0.3g'
union all select '0.2mg'
union all select '0.2mg'
union all select '0.2mg'
union all select '0.2mg'
union all select '0.2mg'
union all select '0.2mg'
union all select '5mg'
union all select '5mg'
union all select '5mg'
union all select '5mg'
union all select '5mg'
union all select '5mg'
union all select '5mg'
union all select '5mg'
union all select '8.8mg'
union all select '8.8mg'
union all select '8.8mg'
union all select '8.8mg'
union all select '8.8mg'
union all select '8.8mg'
union all select '8.8mg'
union all select '8.8mg'
union all select '0.1g'
union all select '0.1g'
union all select '0.1g'
create Function [dbo].[fn_Number] (@texto varchar(100))
returns varchar(100)
as
begin
declare @s varchar(100),@i int
declare @v varchar(1)
set @i=1
set @s=''
while @i<=len(@texto)
begin
set @v=substring(@texto,@i,1)
set @s=@s+case when isnumeric(@v)=1 then @v else '' end
set @i=@i+1
end
return @s
End
select [dbo].[fn_Number](durg_spec) from #tb
--结果
/*
0.25
0.25
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
2.5
2.5
2.5
2.5
0.15
0.15
0.3
0.3
0.2
0.2
0.2
0.2
0.2
0.2
5
5
5
5
5
5
5
5
8.8
8.8
8.8
8.8
8.8
8.8
8.8
8.8
0.1
0.1
0.1
*/
;with tb(durg_spec)
as
(
select '0.25ug' union all
select '0.25ug' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '2.5mg' union all
select '2.5mg' union all
select '2.5mg' union all
select '2.5mg' union all
select '0.15g' union all
select '0.15g' union all
select '0.3g' union all
select '0.3g' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '5mg'
)
--方法2
select cast(left(durg_spec,patindex('%[a-z]%',durg_spec)-1) as float) as durg_spec
from tb
/*
durg_spec
0.25
0.25
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
2.5
2.5
2.5
2.5
0.15
0.15
0.3
0.3
0.2
0.2
0.2
0.2
0.2
0.2
5
*/
;with tb(durg_spec)
as
(
select '0.25ug' union all
select '0.25ug' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '0.5g' union all
select '2.5mg' union all
select '2.5mg' union all
select '2.5mg' union all
select '2.5mg' union all
select '0.15g' union all
select '0.15g' union all
select '0.3g' union all
select '0.3g' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '0.2mg' union all
select '5mg'
)
select cast(substring(durg_spec,1,patindex('%[a-z]%',durg_spec)-1) as float) as durg_spec
from tb
/*
durg_spec
0.25
0.25
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
2.5
2.5
2.5
2.5
0.15
0.15
0.3
0.3
0.2
0.2
0.2
0.2
0.2
0.2
5
*/