?
Solved

sql string insert at position

Posted on 2012-03-26
4
Medium Priority
?
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

752 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