4

I have an sql table named Customers. In Customers I have the column Name.

I want to add in the column Name an space after 5 characters.

Example:

From the column "Name" with the content "name1234" i want add a space at the position 4 so that the result is "name 1234".

I have try it so but it dont work.

SELECT INSERT('Name',4,0, ' ')

FROM
  Customers 

How would you do it, please help.

2
  • Try removing the quotes around the column name ('Name' -> Name). Commented Nov 23, 2015 at 15:06
  • hmm still the same, i got the error "near INSERT syntax error" Commented Nov 23, 2015 at 15:21

4 Answers 4

4

What you want is to update your column values with a concatenation of the first part of the name followed by the space ' ', followed by the rest of the name.

UPDATE Customers
 SET name = CONCAT(SUBSTRING(name, 1, 5), 
     ' ', 
     SUBSTRING(name, 6));
Sign up to request clarification or add additional context in comments.

5 Comments

Okay i have tried it but i get an error = "near FROM syntax error"
try again. I've changed the last substring instruction.
Okay i have tryed now the error is = "no such function SUBSTRING"
Well. What is the version of mysql you are using?
Okay i get it worked now, i have taken the latest mysql:D thanks
0

Check this: http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_insert

SELECT INSERT('Customers', 6, 0, ' ');

To update all rows:

UPDATE YOURTABLE
SET YOURCOL = INSERT(Customers, 6, 0, ' ');

UPDATE

Here is an example:

mysql> select * from CUSTOMERS;
+----+---------+-----+---------+--------+
| ID | NAME    | AGE | ADDRESS | SALARY |
+----+---------+-----+---------+--------+
|  1 | Name456 |   0 |         |   0.00 |
+----+---------+-----+---------+--------+
1 row in set (0,02 sec)

mysql> UPDATE CUSTOMERS SET Name = INSERT(Name, 6, 0, ' ');
Query OK, 1 row affected (0,05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from CUSTOMERS;
+----+----------+-----+---------+--------+
| ID | NAME     | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+--------+
|  1 | Name4 56 |   0 |         |   0.00 |
+----+----------+-----+---------+--------+
1 row in set (0,00 sec)

7 Comments

Here is an example: mysql> SELECT INSERT('Name456', 6, 0, ' '); +------------------------------+ | INSERT('Name456', 6, 0, ' ') | +------------------------------+ | Name4 56 | +------------------------------+ 1 row in set (0,00 sec)
let us start from very beginning: what is a purpose of such modifying? Just change a column name? if yes: ALTER TABLE table_name RENAME COLUMN old_name to new_name;
No i want change the content like i mentioned up. From the column "Name" with the content "name1234" i want add a space at the position 4 so that the result is "name 1234"
try command from the post: UPDATE Customers SET Name = INSERT(Name, 6, 0, ' '); I've just updated the post with an example.
Okay i have tried it but i get an error = "near FROM syntax error"
|
0
WITH  My_string(some_text) AS (VALUES ('This is some text') )

SELECT

   some_text,

   CAST ( (WITH RECURSIVE Counter(x) AS ( VALUES ( ( 1 ) ) UNION ALL SELECT x+1 FROM Counter WHERE x < length(some_text) )

SELECT GROUP_CONCAT(substr(some_text, x, 1),' ') FROM counter)
       AS TEXT) Some_text_With_Spaces

FROM My_string

You are getting the length of the string and creating a temp table with a count from 1 to the length then using GROUP_CONCAT with SUBSTR to pull each character out and use the specified delimiter. In the above example a space.

Comments

-1
SELECT STUFF(name, 5, 0, ' ') FROM Customers 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.