Solved

SQL REPLACE Statement

Posted on 2008-10-07
5
410 Views
Last Modified: 2011-10-03
Hi,

I am trying to update a Column within a Table:

all records in column are in following format:

99-xxx-xxxx
89-xxx-xxxx
99-xxx-xxxx

I just want to update first two char '99' to '89' and rest of '-xxx-xxxx' remain intact.

Thank you in advance.

CN
0
Comment
Question by:CyberNerd
  • 4
5 Comments
 
LVL 38

Expert Comment

by:Jim P.
ID: 22664306
Update MyTable
SET MyCol = '99' + SUBSTR(MyCol, 3, 15)
WHERE Left(MyCol,2) = '89'
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 22664315
Bqckwards
Update MyTable

SET MyCol = '89' + SUBSTR(MyCol, 3, 15)

WHERE Left(MyCol,2) = '99'

Open in new window

0
 
LVL 2

Author Comment

by:CyberNerd
ID: 22664393
I've ran the update statement but I get the following error:

Msg 195, Level 15, State 10, Line 2
'SUBSTR' is not a recognized built-in function name.

Is this mean SUBSTR is not enabled in my SQL 2005 server?
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 125 total points
ID: 22664514
Try substring. SUBSTR is an accepted synonym in Oracle. My bad.
Update MyTable

SET MyCol = '89' + substring(MyCol, 3, 15)

WHERE Left(MyCol,2) = '99'

Open in new window

0
 
LVL 38

Expert Comment

by:Jim P.
ID: 22664870
Glad to be of assistance. May all your days get brighter and brighter.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

896 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

13 Experts available now in Live!

Get 1:1 Help Now