Tim Levinsson
Tim Levinsson

Reputation: 597

Insert MySQL Query with use of concat

Im trying to insert a query to my database using a name in a variable and also using a name in the values. So i want to make it in to 1 name.

Here is an example

SET @name = "This is my";
INSERT INTO mytable (id, name)
VALUES (1, @name + "Test Query");

So, This query should then insert a row with id 1 and name "This is my Test Query" But it gives me an error. Truncated incorrect DOUBLE value: 'Test Query'

Upvotes: 6

Views: 24400

Answers (2)

Ajay K
Ajay K

Reputation: 61

i found no error in sql.. it executed without error..

DECLARE @name as varchar(100)  
DECLARE @mytable  as table ( id int, [name] varchar(100))  

SET @name = 'This is my';

INSERT INTO @mytable (id, name)
VALUES (1, @name + 'Test Query');

Select * from @mytable

Upvotes: 0

Armen
Armen

Reputation: 4192

You can't use @name + "Test Query" syntax instead you shoud use concat() mysql function see here for more info:http://www.w3resource.com/mysql/string-functions/mysql-concat-function.php

SET @name = "This is my";
INSERT INTO `names` (id, name)
VALUES (1, concat(@name, 'Test Query')); 

also make sure that your id is not autoincreament if it is then no need to pass id number in your query like this

INSERT INTO `names` (name)
VALUES (concat(@name, 'Test Query')); 

Upvotes: 7

Related Questions