0

I have a database table, with a column that contains integers. Each entry is a phone number, and they are all missing a zero at the beginning.

e.g. I have 798514586, 785558999

I want to run a SQL query that will modify each entry by putting a zero infront of it.

So the result will be 0798514586, 0785558999

IS there such a query to do this?

Mark N Hopgood
  • 843
  • 8
  • 14

5 Answers5

2

Try this

Syntax:

UPDATE <table> SET <column_to_update> = CONCAT(<string_to_concat>, <column_to_update>)

Example:

UPDATE contacts SET phone = CONCAT('0', phone)
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
1

I suppose you dont't want to add leading zero if it already exists:

update TableName
set SomeColumn = concat('0', SomeColumn)
where SomeColumn not like '0%'
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75
1

It's not a good idea to store phone numbers as INTs, it's better to use a VARCHAR here. I would suggest you do add a new varchar column:

ALTER TABLE numbers ADD COLUMN phone_new VARCHAR(15);

then you can use an UPDATE query:

UPDATE numbers
SET
  phone_new = CONCAT('0', phone)

MySQL will automatically cast the number to a string, and using CONCAT you can add a leading zero.

fthiella
  • 48,073
  • 15
  • 90
  • 106
0

You can try by this:

update tableName set fieldName = CONCAT('0',fieldName)
Riken Shah
  • 87
  • 10
0

You can use LPAD :

Update _table set _col  = LPAD(_col , 10, '0');
cedric
  • 302
  • 1
  • 12