[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

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