Solved

SQL - substring query assistance

Posted on 2015-01-16
3
106 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 15

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server concatenate fields 10 36
Sql server function help 15 31
SQL Server - Set Field Values ito Zero Based on Related Table 4 28
SQL Error - Query 6 26
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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