У меня есть запрос, который возвращает более 100 000 строк в наборе результатов, например, для
ID Description
1 This site uses cookies to deliver our services and to show you relevantads and job listings. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and term
2 RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access .The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.
3 ................
4 .............
5
and so on
Я использовал эту функцию, чтобы добавить любую специальную после n-й позиции:
CREATE FUNCTION dbo.fn_BreakString (@STR VARCHAR(MAX), @CHARLEN INT)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @NEW_STR VARCHAR(MAX), @POS INT, @LEN INT
SET @NEW_STR = ''
SET @POS = 1
SET @LEN = LEN(@STR)
WHILE @POS < @LEN
BEGIN
IF @POS = 1
BEGIN
SET @NEW_STR = SUBSTRING(@STR, @POS, @CHARLEN)
END
ELSE
BEGIN
SET @NEW_STR = @NEW_STR + @#$ + SUBSTRING(@STR, @POS, @CHARLEN)
END
SET @POS = @POS + @CHARLEN
END
RETURN @NEW_STR
END
Но яполучаю вывод, как это:
select
id, dbo.fn_BreakString(description, 10) desc
from
tablea
Вывод:
ID Description
1 This site u@#$ses cookie@#$s to deliver@#$our servi@#$ces and to show you @#$relevantads and job listings.including the Stack Overflow Network, is subject to these policies and term
2 RDBMS is t@#$he basis fo@#$r SQL, and @#$for all modern database systems such as MS SQL Server
3 ................
4 .............
5
and so on
Я пытаюсь добавить специальное после n-й позиции без пробивного слова, это может быть до или после n-й позиции, это нормально.
Требуемый вывод:
ID Description
1 This site@#$uses cookies@#$ to deliver our@#$ services and to show you relevant ads and job listings. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms
2 RDBMS is@#$the basis@#$for SQL, and @#$for all modern database systems such as MS SQL Server
3 ................
4 .............
5
and so on
Я пробовал его ниже сценария, иногда он работает, а иногда идет в бесконечном цикле
DECLARE @NEW_STR NVARCHAR(MAX), @POS INT, @LEN INT, @End INT, @NEW_STRend NVARCHAR(MAX) = ''
SET @NEW_STR = ''
SET @POS=1
SET @LEN = LEN(@STR)
SELECT @LEN
WHILE @POS < @LEN
BEGIN
IF @POS = 1
BEGIN
PRINT @pos
SET @End = LEN(LEFT(@Str, @CHARLEN)) - CHARINDEX(' ', REVERSE(LEFT(@Str, @CHARLEN)))
PRINT @End
SET @NEW_STR = SUBSTRING(@STR, @POS,@End )
END
ELSE
BEGIN
SET @End = LEN(LEFT(@Str, @CHARLEN)) - CHARINDEX(' ', REVERSE(LEFT(@Str, @CHARLEN)))
PRINT @End
SET @NEW_STR = @NEW_STR +@#$+ SUBSTRING(@STR, @POS+1,@End )
END
--select @pos ,@charlen
SET @POS = @POS + @CHARLEN
END
SELECT @NEW_STR