• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

sql string formatting

how do you script the following in sql

012341234566789

to remove the first '0' and replace with '44'
0
amillyard
Asked:
amillyard
1 Solution
 
EvilPostItCommented:
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
 
Ephraim WangoyaCommented:

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
 
jimyXCommented:
use replace('012341234566789','0','44')

Update Table1 Set Col1 = replace(Col1, '0', '44');
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
amillyardAuthor Commented:
EvilPostIt: do u have that as an update / set statement?
0
 
EvilPostItCommented:
UPDATE PhoneDirectory
SET Number='44'+RIGHT(Number,(LEN(NUMBER)-1))
WHERE LEFT(Number,1)='0'

Open in new window

0
 
amillyardAuthor Commented:
spot on - thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now