Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 SQL String limit but not cut off words.

Author  Topic 

raptor260
Starting Member

7 Posts

Posted - 2014-11-05 : 21:41:16
I have a table called notes with a column that is nvarchar(MAX), this columns may have 10 characters or it may have 800. I need to slice this up into 250 character segments.

If this line below was the value in the table I need to create 2 rows with a max of 250 characters. If you count characters you would split the word voicemail. I need to see it is in the middle of a word and not break it up.
Blanket Lien Filed, account returned to Provider. Blah Blah will continue to follow and pursue lien payment. Called Pt Blahat xxx-xxx-xxxx. Left voice mail. Voicemail greeting did not state a name. Called Pt at xxx-xxx-xxxx. Left voice mail.
Voicemail greeting did not state a name.

I need it to look something like this and I have no idea to do this.

Row 1: Blanket Lien Filed, account returned to Provider. Blah Blah will continue to follow and pursue lien payment. Called Pt Blahat xxx-xxx-xxxx. Left voice mail.

Row 2: Voicemail greeting did not state a name. Called Pt at xxx-xxx-xxxx. Left voice mail.
Voicemail greeting did not state a name.

I have tried this but it cuts the words off.
SELECT Acct, SUBSTRING(Notes, 1, 249) as Note, 'A1' AS Prefix
FROM dbo.[RegionalOneNotesResults]
UNION
SELECT Acct, SUBSTRING(Notes, 250, 249) as Note, 'B2' AS Prefix
FROM dbo.[RegionalOneNotesResults]

Any help would be appreciated

Ifor
Aged Yak Warrior

700 Posts

Posted - 2014-11-06 : 11:39:37
As t-sql is not really designed for string handling, I would be inclined to look at writing a CLR function. You could try google for this but it is well beyond a forum post.

If you really want to use t-sql, you could try recursion if the length of the notes is not too long. Otherwise you will have to use iteration.
The following is an outline example of recursion:

CREATE TABLE #t
(
Acct int NOT NULL
,Note nvarchar(MAX) NOT NULL
);
INSERT INTO #t
VALUES (1, 'Blanket Lien Filed, account returned to Provider. Blah Blah will continue to follow and pursue lien payment. Called Pt Blahat xxx-xxx-xxxx. Left voice mail. Voicemail greeting did not state a name. Called Pt at xxx-xxx-xxxx. Left voice mail.
Voicemail greeting did not state a name.');

WITH RowNotes
AS
(
SELECT Acct
,1 AS NoteRow
,LEFT(Note, SplitIndex) AS Note
, SUBSTRING(Note, SplitIndex + 1, 1000000) AS RestNote
FROM
(
SELECT Acct, Note
,CASE
WHEN NoteLen < 250 THEN NoteLen
ELSE NoteLen + 1 - PATINDEX('%[^0-9,A-Z,a-z,-]%', REVERSE(LEFT(Note, 250)))
END AS SplitIndex
FROM
(
SELECT Acct, Note
,LEN(LEFT(Note, 250)) AS NoteLen
FROM #t
) D1
) D

UNION ALL

SELECT Acct
,NoteRow
,LEFT(Note, SplitIndex) AS Note
, SUBSTRING(Note, SplitIndex + 1, 1000000) AS RestNote
FROM
(
SELECT Acct, Note, NoteRow
,CASE
WHEN NoteLen < 250 THEN NoteLen
ELSE NoteLen + 1 - PATINDEX('%[^0-9,A-Z,a-z,-]%', REVERSE(LEFT(Note, 250)))
END AS SplitIndex
FROM
(
SELECT Acct, RestNote As Note
,NoteRow + 1 AS NoteRow
,LEN(LEFT(RestNote, 250)) AS NoteLen
FROM RowNotes
WHERE LEN(RestNote) > 0
) D1
) D
)
SELECT Acct, NoteRow, Note
FROM RowNotes
ORDER BY Acct, NoteRow;

Go to Top of Page
   

- Advertisement -