• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: SQL: Divide long text in multiple rows

Resolved: SQL: Divide long text in multiple rows

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I would like to divide a long text in multiple rows; there are other questions similar to this one but none of them worked for me. What I have
What I would like to do is divide that string every n characters Result if n = 15:
Even better if the split is done at the first space after n character.
I tried with string_split and substring but I cannot find anything that works. I thought to use something similar to this:
But it doesn’t take into account the length and I don’t like casting a varchar variable into a super.

Answer:

You can use generate_series() to accomplish this:
Splitting on spaces after the length is a little trickier. We would have to split the message into words and then figure out how to break them into groups and then reaggregate.
I could not figure out how to split on spaces without recursion. I hope you don’t mind that it treats all whitespace as word boundaries:
db<>fiddle here
Edit to add:
Can you please tell me whether the below works in Redshift?

with gs as (
select generate_series as posn
from generate_series(1, 150000, 15)
)
select *, substring(m.message, gs.posn, 15) as split_message
from messages m
join gs
on gs.posn <= greatest(1, length(m.message)) order by m.id, gs.posn ; [/code]

If you have better answer, please add a comment about this, thank you!

postgresql sql
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: When converting markdown to latex with pandoc, how can I end a block before the next heading?

26/03/2023

Resolved: TYPO3 SQL error: Field ‘tx_imagezoom_set’ doesn’t have a default value

26/03/2023

Resolved: std::regex_replace to replace multiple combinations

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.