Solved

sql string formatting

Posted on 2011-03-24
6
329 Views
Last Modified: 2012-05-11
how do you script the following in sql

012341234566789

to remove the first '0' and replace with '44'
0
Comment
Question by:amillyard
6 Comments
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 35208239
Assuming this is a select statement from a table...


SELECT Number AS NumberBefore, 
CASE LEFT(Number,1)
WHEN '0' THEN '44'+RIGHT(Number,(LEN(NUMBER)-1))
ELSE Number
END AS NumberAfter
FROM PhoneDirectory

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35208242

you can do
select '44' + SUBSTRING('012341234566789', 2, LEN('012341234566789'))

or
select REPLACE('012341234566789', '0', '44')  -- but this will replace all occurrences of 0
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35208247
use replace('012341234566789','0','44')

Update Table1 Set Col1 = replace(Col1, '0', '44');
0
Zoho SalesIQ

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

 

Author Comment

by:amillyard
ID: 35208732
EvilPostIt: do u have that as an update / set statement?
0
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 500 total points
ID: 35208743
UPDATE PhoneDirectory
SET Number='44'+RIGHT(Number,(LEN(NUMBER)-1))
WHERE LEFT(Number,1)='0'

Open in new window

0
 

Author Closing Comment

by:amillyard
ID: 35209417
spot on - thank you.
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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

16 Experts available now in Live!

Get 1:1 Help Now