declare @aa nvarchar(4000) set @aa='select * from tableName where id = "1";' select replace(substring(@aa,CHARINDEX(' from ',@aa)+6,CHARINDEX(' where ',@aa)-CHARINDEX(' from ',@aa)-6),' ','') --result tableName
the similiar answers is like HTTP://www.ebigear.com/Fund/PlayNews.php?NewsID=29966&ID=285885
declare @aa nvarchar(4000)---借用 set @aa='select * from tableName where id = "1";' select @aa=ltrim(stuff(@aa,1,charindex('from',@aa)+3,'')) select left(@aa,charindex(' ',@aa)-1) 合并成一句(只考虑from,没有where也行) select left(ltrim(stuff(@aa,1,charindex('from',@aa)+3,'')),charindex(' ',ltrim(stuff(@aa,1,charindex('from',@aa)+3,'')))-1)
set @aa='select * from tableName where id = "1";'
select replace(substring(@aa,CHARINDEX(' from ',@aa)+6,CHARINDEX(' where ',@aa)-CHARINDEX(' from ',@aa)-6),' ','')
--result
tableName
set @aa='select * from tableName where id = "1";'
select @aa=ltrim(stuff(@aa,1,charindex('from',@aa)+3,''))
select left(@aa,charindex(' ',@aa)-1)
合并成一句(只考虑from,没有where也行)
select left(ltrim(stuff(@aa,1,charindex('from',@aa)+3,'')),charindex(' ',ltrim(stuff(@aa,1,charindex('from',@aa)+3,'')))-1)