I need a function that outputs first N number of characters in a dash-delimited input string.
Requirements:
-
If N drops in the middle of a word, include the last word in the output even if total gets more than N
-
If the output ends with “-” AND LEN(output) == N , then include next word
Example: (N = 70)
declare @Text varchar(1000) = 'this-is-product-url-prepared-for-better-Google-Search-Engnine-SEO-totalLength-should-be-70-characters'
I have already taken the first 70 characters, which results in the following (I remove ending dash – in a separate step not included here):
SELECT LEFT(@Text + '-', CHARINDEX('-',@Text, 70))
this-is-product-url-prepared-for-better-Google-Search-Engnine-SEO-tot
I need a function that returns the following in this case:
this-is-product-url-prepared-for-better-Google-Search-Engnine-SEO-totalLength
Any solutions will be appreciated.
2
Answers
Thanks @LukStorms
I achieved this by the following statement:
UPDATED ANSWER: Fixed your expression
This will work and is based on what @Lukstroms posted; his does not handle your second requirement (when @N is the position of a hyphen.)
PREVIOUS ANSWER:
Here’s how you could do something like this using ngrams8k This will handle situations where @N represents the middle of the word or a dash
-
.FINAL UPDATE (NOTE ABOUT N-GRAMS)
As Martin correctly mentioned, the N-Grams solution is overly complex but I was in a rush and couldn’t fix the OP’s original CHARINDEX expression. That said, ngrams8k solution is nasty fast allows a bunch of flexibility.
For example, let’s say the requirement included an upper and lower bound parameter; e.g. we needed everything between the first hyphen higher than
@Low
and the last hyphen lower than@High
. This can be handled using a minor tweak to the answer above.Returns: