SQL 循环截取指定位置字符串

declare @i int //声明变量

set @i=1
while @i<=154  //需要处理的数据总数(行为单位)
begin
update [weilaixueyuan_db].[dbo].[Jft_xiaofangZH_Classify] set Content=(
select 
SUBSTRING ((select name from [weilaixueyuan_db].[dbo].[Jft_xiaofangZH_Classify] where Id=@i), 0 , 
(select CHARINDEX(' ',
(select name from [weilaixueyuan_db].[dbo].[Jft_xiaofangZH_Classify] where Id=@i),
0))) ) where Id=@i ;
set @i=@i+1
end



//上面是普通的sql更新语句,其中有两个字符串处理方法:
SUBSTRING ( character_expression , start , length )  
  1. 函数说明:SUBSTRING ( '源字符串' , '截取起始位置(含该位置上的字符)' , '截取长度' )  
  2. 返回字符、binary、text 或 image 表达式的一部分  
  3. select SUBSTRING('SQL_Server_2008',5 ,6);  
  4. 返回结果:Server
//PS 索引从1开始
2.CHARINDEX('需要匹配的字符串','源字符串','匹配位置')
select CHARINDEX('d','abcdefg',0)--4

  

原文地址:https://www.cnblogs.com/ankeyliu/p/5606216.html