Solved

SQL - substring query assistance

Posted on 2015-01-16
3
105 Views
Last Modified: 2015-01-19
Hi experts,

I am trying to write a query that replaces the very first instance of the word 'Cath' in a string, with the word 'Echo'.

So far I have:

  update  card_proceduresextpop_
set reportdisplay =  REPLACE(SUBSTRING(reportdisplay, 1, CHARINDEX(reportdisplay,'Echo',1)), 'Echo', 'Cath')
         , SUBSTRING(reportdisplay, CHARINDEX(reportdisplay,'Echo',1)+1, LEN(reportdisplay))
  FROM card_proceduresextpop_
  where procedureabbr = 'Echo'
and substring(reportdisplay, 1, 4) = 'Echo'

but it gives me an error:

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.


Any ideas?

Thanks
0
Comment
Question by:robthomas09
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 400 total points
ID: 40554533
--replaces the very first instance of the word 'Cath' in a string, with the word 'Echo'
--....that's what I did, although your code does the opposite...
--...swap 'Cath' for 'Echo' and vice-versa in the string below if you need to

update  card_proceduresextpop_
set reportdisplay =  stuff(reportdisplay, CHARINDEX('Cath', reportdisplay), 4, 'Echo')
where reportdisplay like '%Cath%'
0
 
LVL 14

Assisted Solution

by:Vikas Garg
Vikas Garg earned 100 total points
ID: 40554940
Hi,

You can try this logic

DECLARE @reportdisplay VARCHAR(40) = 'HELLO Cath Cath'

SELECT STUFF(@reportdisplay,CHARINDEX('Cath',@reportdisplay,1),LEN('Cath'),'Echo')

Open in new window

0
 

Author Closing Comment

by:robthomas09
ID: 40558139
Thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now