0

I have a column which can contain up to 500 characters (Message Varchar (500)).

I need to insert a Caret (^) at position 250, or if there are less then 250 characters i.e. 120, then at the last characters position.

Example 497 characters;

strong textFar far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows b^y their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographi.

The Caret is inserted at characters 250 which splits the word "by" into "b^y".

Example 140 characters;

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in.^

As this example has less than 250 characters, then the Caret is inserted at the end of the message.

The max characters that will ever be inputted will be 499, therefore leaving 1 character for the inserting of the Caret.

Any help would be grateful.

2
  • 2
    Which RDBMS are you using? String manipulation function are largely platform specific. Commented Nov 29, 2017 at 9:55
  • SQL server 2012. Thanks. Commented Nov 29, 2017 at 9:57

1 Answer 1

4

Quite simple with CASE and STUFF:

-- @t is assumed to contain the text to be changed

CASE WHEN LEN(@t) > 250
    THEN STUFF(@t,250,0,'^')
    ELSE @t + '^' END
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. It's easy when you know how. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.