sql server - Show row from block of string without cursors or while loops -
i have variable containig numerous of lines string looking that:
senteqweqence text sentence -1 text sendasdadadastence other text
my goal extract first line variable cointaining string -1
regardless of text above or below line, without using cursors or while loops.
expected result: sentence -1 text
what have tried far:
declare @sql nvarchar(max), @end int, @start int set @sql = 'senteqweqence text sentence -1 text sendasdadadastence other text' set @end = charindex(char(10),@sql,charindex('-1',@sql)) set @start = charindex(char(10),reverse(@sql),1) print substring(@sql,@start,@end-@start)
something should going. might need add or subtract character @ either end of substring:
set @end = charindex(char(10),@sql,charindex('-1',@sql)) set @start = @end - charindex(char(10),reverse(left(@sql,@end)),charindex('1-',reverse(left(@sql,@end))))+2 print substring(@sql,@start,@end-@start)
Comments
Post a Comment