Guido Guido - 4 months ago 8
SQL Question

Changing a specific char in a MySQL string

I have a table with a series of permissions, and I need to change every user's permission to Y in a given position p in the string, is there a SQL command I can use or do I have to write a program/script to do it?

Answer

You can use a combination of concat and substring in MySQL:

mysql> select concat(substring('12345',1,3),'A',substring('12345',5));
+---------------------------------------------------------+
| concat(substring('12345',1,3),'A',substring('12345',5)) |
+---------------------------------------------------------+
| 123A5                                                   |
+---------------------------------------------------------+

You can replace '12345' with the name of your column.

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

Comments