Skip to content
Advertisement

Split text into multiline chunks based on fixed character length using SQL

I am looking for SQL that takes a string of text (coming in as up to 3 lines, but usually 1) and breaks it into at most 3 lines with a max of 30 characters per line. (The background is I’m trying to insert text into address line fields and sometimes the incoming values are longer than 30 characters and need to split into smaller chunks and bumped down to another line).

I’m not even sure where to start. Since it’s address text there’s quite a bit of variance and ideally it shouldn’t split in the middle of a word, if possible. This would be in SQL Server 2012. Any suggestions?

Advertisement

Answer

This should accomplish what you want using recursive CTEs:

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement