请教:数字和字母间如何插入空格?
一个字段中的纪录如下:
1.59LINEN 23ACRYLIC 18COTTON
2.100COMBED COTTON
3.75SILK 25COTTON
4.50ACRYLIC 50MOHAIR
...
想在每个数字和字母之间插入空格:
1.59 LINEN 23 ACRYLIC 18 COTTON
2.100 COMBED COTTON
3.75 SILK 25 COTTON
4.50 ACRYLIC 50 MOHAIR
...
该如何实现啊?
------解决方案--------------------declare @t table (
s varchar(50)
)
insert @t select
'1.59LINEN 23ACRYLIC 18COTTON '
union all select
'2.100COMBED COTTON '
union all select
'3.75SILK 25COTTON '
union all select
'4.50ACRYLIC 50MOHAIR '
while exists (select 1 from @t where PATINDEX ( '%[0-9][a-z]% ' , s )> 0)
update @t
set s=stuff(s,25,0, ' ')
where PATINDEX ( '%[0-9][a-z]% ' , s )> 0
select * from @t
--结果
s
--------------------------------------------------
1.59 LINEN 23 ACRYLIC 18 COTTON
2.100 COMBED COTTON
3.75 SILK 25 COTTON
4.50 ACRYLIC 50 MOHAIR
(所影响的行数为 4 行)
------解决方案--------------------declare @s varchar(8000),@i int
set @s= '59LINEN 23ACRYLIC 18COTTON '
while patindex( '%[0-9][A-Z]% ',@s)> 0
select @i=patindex( '%[0-9][A-Z]% ',@s),@s=left(@s,@i)+ ' '+right(@s,len(@s)-@i)
select @s
------解决方案--------------------DECLARE @tb TABLE(id INT,string VARCHAR(8000))
INSERT INTO @tb SELECT 1, '59LINEN23ACRYLIC18COTTON '
UNION ALL SELECT 2, '100COMBED COTTON '
UNION ALL SELECT 3, '75SILK 25COTTON '
UNION ALL SELECT 4, '50ACRYLIC 50MOHAIR '
DECLARE @i INT
DECLARE @s VARCHAR(100)
DECLARE @OrderID VARCHAR(50)
DECLARE StrCursor CURSOR FOR SELECT id,string FROM @tb
OPEN StrCursor
FETCH NEXT FROM StrCursor INTO @OrderID,@s
WHILE @@FETCH_STATUS = 0
BEGIN
WHILE PATINDEX( '%[0-9][A-Z]% ',@s)> 0
SELECT @i=PATINDEX( '%[0-9][A-Z]% ',@s),@s=LEFT(@s,@i)+ ' '+RIGHT(@s,LEN(@s)-@i)
UPDATE @tb SET string = @s WHERE ID=@OrderID
FETCH NEXT FROM StrCursor INTO @OrderID,@s
END
CLOSE StrCursor
DEALLOCATE StrCursor
SELECT * FROM @tb
/*
id string
---------------------------------------
1 59 LINEN23 ACRYLIC18 COTTON
2 100 COMBED COTTON
3 75 SILK 25 COTTON
4 50 ACRYLIC 50 MOHAIR
*/