4

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

4 Answers 4

6

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 
Sign up to request clarification or add additional context in comments.

Comments

3

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))

1 Comment

Thank you! Stupid mistake I was doing "LM" not 'LM'
1

Create a view vwID:

CREATE VIEW vwID AS
SELECT "LM" + ID AS ID, <list the rest of your columns here>
;

Comments

1

CONCAT() should work fine:

update [TABLE_NAME] set col=CONCAT('LM', col) where col is not NULL

1 Comment

love ti, since this solution can be used with SQLAlchemy

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.