I need to split a nvarchar(100) column into three nvarchar(28) columns without a known delimiter and without breaking mid-word. I am thinking that I would need to find the space that is near the 28th character, measure length and position of the word just before that space and decide weather the delimiter should be before or after that word. Then, again for the 3rd column.
From:
Col ------------------------------------------------------------------------------------- Royal Mission Open Hutch with 4-Arch Doors, Plain Glass (with Glass Shelves Standard)
To:
Col1 Col2 Col3 ------------------------ ------------------------- -------------------------- Royal Mission Open Hutch 4-Arch Doors, Plain Glass (with Glass Shelves Standa
Any Ideas?
Thanks nab
I am using SQL 2008
Using CROSS APPLYs, you could try something like this:
The first CROSS APPLY searches for the last space in the first 29 characters of the big column, and the second one uses the found position to produce the first smaller column,
col1and return the rest of the string ascol23.The next two CROSS APPLYs do perform the same manipulations on
col23, thus producingcol2andcol3. The only difference is, the last CROSS APPLY puts intocol3at most 28 characters rather than all the remaining ones.The hardcoded values like
28,29and30could be parametrised, but I’ll leave that part of the job to you.You can try this query at SQL Fiddle.