--创建函数
create FUNCTION dbo.Ufn_StringSplit
(@s as varchar(2000)) RETURNS int
AS
BEGIN
declare @r as int,@CHARINDEX as int
set @r=0
declare @i as int
set @i=0
while @i<10
begin
set @CHARINDEX= CHARINDEX(cast(@i as varchar(1)),@s)
if @CHARINDEX<>0
begin
if @CHARINDEX<@r or @r=0
set @r=@CHARINDEX
end
set @i=@i+1
end
return @r
END
GO
--创建测试数据
select * into # from (
select '0101010001' a,null b,'0101010001' c union all
select 'IPY88866038','IPY','88866038' union all
select 'IPY-HCAA200038401','IPY-HCAA','200038401' union all
select 'DAA05007220600T','DAA','05007220600') aa
--执行查询
select
a
,case when position=1 then null else left(a,position-1) end as 转换列1
, right(a,len(a)-position+1) as 转换后列2
from
(select a,dbo.Ufn_StringSplit(a) as position from #) as abcd
--删除临时数据
drop table #
drop FUNCTION dbo.Ufn_StringSplit
在SQL2014中测试通过
如图:
你是oralce还是mysql啊
假设是oracle,表为t,列为a,你试一下,下边这个行不行,不行再问我
select t.a,
(case
when (t.a=regexp_replace(t.a,'\D*',''))
then null
else regexp_replace(t.a,'\d*','') end) a1,
regexp_replace(t.a,'\D*','') a2
from t
select replace(原始列,转换列,'')
什么数据库