Reversing words in each line a field text contains several lines in a table

Viewed 30

I have a field in table SQL contains text with several line ,the words of each line are inverted similar to this:

“it of face the on , Therefore . people English be should answer the be won’t England from person a ,said That of Australian an from different visibly their open they until ancestry European .accent English an have and mouths”

How can it be corrected :

“Therefore, on the face of it the answer should be English people. That said, a person from England won’t be visibly different from an Australian of European ancestry until they open their mouths and have an English accent.”

2 Answers

using functions such as SUBSTRING,CHARINDEX for splitting string and REVERSE to reverse your string in SQL could be beneficial.however,your string is very complicated.

using function SUBSTRING,CHARINDEX in WHILE loop on string and cursor in table field

declare @str nvarchar(max)
declare @hokm bigint 
declare @enter_tedad int
declare cur cursor 

for select  id,EPDesc,LEN(CAST (EHPDesc AS NVARCHAR(MAX)))
    - LEN(REPLACE(CAST (EPDesc AS NVARCHAR(MAX)), CHAR(13), '')) from table1 where EPDesc!=''  and EPDesc2 is null
    

open cur
fetch next from cur into @hokm,@str,@enter_tedad
print @hokm
while @@fetch_status = 0
begin

DECLARE @enter13_10 INT ,
    @str_tmp NVARCHAR(MAX)= N'' 
WHILE @enter_tedad > 0
    OR LEN(@str) > 0
BEGIN   
    SET @enter13_10 = CHARINDEX(CHAR(13), @str, 1);

    PRINT '@enter13_10';    
    PRINT @enter13_10;

    IF @enter13_10 > 0
    BEGIN       
        SET @str_tmp = SUBSTRING(@str, 1,
                                 CHARINDEX(CHAR(13), @str, @enter13_10))
            +' '+ @str_tmp;

        SET @str = SUBSTRING(@str, CHARINDEX(CHAR(13), @str, @enter13_10) + 1,
                             800);
    END;
    ELSE
    BEGIN       
        SET @str_tmp = @str +' '+ @str_tmp;
    
        SET @str = '';

    END;
        SET @enter_tedad = @enter_tedad - 1;
END;
print @str_tmp
update table1 set EPDesc2=@str_tmp where id=@hokm
set @str_tmp=N''

    fetch next from cur into @hokm,@str,@enter_tedad
end
close cur
deallocate cur
Related