Solved

sql string insert at position

Posted on 2012-03-26
4
326 Views
Last Modified: 2012-04-12
Is there any SQL funtion that allows me to insert a string at a specific position within a string?  As in if I have the string "Hello" Can I insert the text "y je"text specifically at position 3 to achieve the result "Hey jello"
0
Comment
Question by:zintech
  • 2
4 Comments
 
LVL 2

Expert Comment

by:dantheanswerman
ID: 37768178
0
 
LVL 9

Expert Comment

by:OCDan
ID: 37768305
Although STUFF is very useful and well worth reading about, it REPLACES characters instead of just inserting in the desired place.

Here are a couple of ways to do it, CHARINDEX is more flexible here and you can change it as you to insert after whatever character/sequence you want.:

declare @string varchar(200)
set @string = 'hello'

select left(@string,2)+'y je'+ right(@string,3)

--OR

select substring(@string,0, len(@string)-charindex( 'e',@string)) + 'y je' + substring(@string, charindex( 'e',@string)+1,len(@string))

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 37769531
>>it REPLACES characters instead of just inserting in the desired place.<<
Actually that is not true.  It can insert.  You may want to re-read the documentation or see for yourself:
DECLARE @Value varchar(20) = 'Hello'
SELECT STUFF(@Value, 3, 0, 'y je')

Open in new window

Output:
Hey jello
0
 
LVL 9

Expert Comment

by:OCDan
ID: 37769948
My bad, thanks acperkins
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question