Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepend (add character) to string in sql server

Tags:

sql

sql-server

I have a column in a SQL table called ID (ex. 1234,12345). I want to add a "LM" to every record in that column: LM1234, LM12345, etc

like image 364
Kevin M Avatar asked Oct 29 '25 11:10

Kevin M


2 Answers

We suppose that ID column has varchar/char/... or any other string related data type., so try this:

UPDATE [TABLE_NAME] SET [COL] = 'LM'+COL 
like image 85
Vahid Farahmandian Avatar answered Nov 01 '25 07:11

Vahid Farahmandian


Assuming that the id is a string, just do an update:

update t
     set id = 'LM' + id;

If column is not a string, then you need to make it one first:

alter table t alter id varchar(255);

update t
     set id = 'LM' + id;

Also, you could just add a computed column to do the calculation:

alter table t add lm_id as (concat('LM', column))
like image 36
Gordon Linoff Avatar answered Nov 01 '25 08:11

Gordon Linoff