当前位置: 代码迷 >> Sql Server >> 求1语句,
  详细解决方案

求1语句,

热度:98   发布时间:2016-04-24 09:11:40.0
求一语句,急!!!!
机具型号
久保田SPW-68C   

机具           型号
久保田      SPW-68C   

上面的是一个字段,我想分成下面这的两个字段!!!!!!!谢谢各位啦
------解决思路----------------------
--什么规律呢,直接根据中英文?
SELECT SUBSTRING(机具型号,1,PATINDEX('%[^吖-座]%',机具型号)-1)[机具]
,SUBSTRING(机具型号,PATINDEX('%[^吖-座]%',机具型号),LEN(机具型号))[型号]
FROM TB

------解决思路----------------------
写个自定义函数

-- =============================================
/*
select * from dbo.FUN_Split('久保田SPW-68C')
*/
-- =============================================
CREATE FUNCTION FUN_Split
(
-- Add the parameters for the function here
@Str VARCHAR(100)
)
RETURNS @temp TABLE(Item1 VARCHAR(50),Item2 VARCHAR(50)) 
AS
BEGIN
declare @i int,@len int
,@tempStr varchar(5)
,@strCN varchar(50),@StrOther varchar(50)
select @len = len(@Str),@i = 1

while @i <= @len
begin
set @tempStr = substring(@Str,@i,1)
if datalength(@tempStr) <> len(@tempStr)
begin
if isnull(@strCN,'') = ''
set @strCN = @tempStr
else
set @strCN = @strCN + @tempStr
end
else
begin
if isnull(@StrOther,'') = ''
set @StrOther = @tempStr
else
set @StrOther = @StrOther + @tempStr
end

set @i = @i + 1
end
insert into @temp values(@strCN,@StrOther)
return
END

  相关解决方案