Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the first character if it is a specific character in SQL

I currently have a table Telephone it has entries like the following:

9073456789101
+773456789101
0773456789101

What I want to do is remove only the 9 from the start of all the entries that have a 9 there but leave the others as they are.

any help would be greatly appreciated.

like image 280
w3n2u Avatar asked Jul 31 '13 15:07

w3n2u


People also ask

How do I remove a specific character from a string in SQL?

The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

How do I remove the first character of a string?

To delete the first character from a string, you can use either the REPLACE function or a combination of RIGHT and LEN functions. Here, we simply take 1 character from the first position and replace it with an empty string ("").

How do I remove a special character from a value in SQL?

Replace function – using Replace function you can remove a special character from a database filed. This function only replaces a single special character.


1 Answers

While all other answer are probably also working, I'd suggest to try and use STUFF function to easily replace a part of the string.

UPDATE Telephone
SET number = STUFF(number,1,1,'')
WHERE number LIKE '9%'

SQLFiddle DEMO

like image 78
Nenad Zivkovic Avatar answered Oct 08 '22 11:10

Nenad Zivkovic