golmschenk
golmschenk

Reputation: 12404

SQL - How to form a string by concatenating semicolons to a number?

Specifically MySQL, but I'm guessing the answer should be generic.

First I'm setting a variable as a number like so:

SET @somenumber:=LAST_INSERT_ID();

Note that LAST_INSERT_ID() is basically MySQL's equivalent of SCOPE_IDENTITY(). Next I try to create a string by concatenating semicolons onto this number:

SET @somestring:=';'+@somenumber+';';

Last I try to insert this as a string:

INSERT INTO `sometable` (`somevarcharcolumn`) VALUES (somestring);

And the result is that only the number is added in the column and the semicolons are missing.

Can anyone point out what I'm doing wrong or what I need to do to make it work? Thank you much!

Upvotes: 0

Views: 2000

Answers (1)

codingbiz
codingbiz

Reputation: 26386

Hope this helps

//MySQL
SET @somestring:=CONCAT(';',@somenumber,';');

//SQL Server
SET @somestring = ';'+CAST(@somenumber AS VARCHAR)+';';

//Oracle
SET somestring_ :=';' || somenumber_ || ';';

Upvotes: 2

Related Questions