How to add a character

I have a table like this:

Item          Code
A             123456
B             123455
C             23457
D             123458
E             23459
F           

The column Codeshould have 6 characters, and I need to add '1' (for example, 23455in 123455) for those elements with less than 6 characters.

How can I do this using SQL?

Thank,

+3
source share
2 answers
Update table
    set Code = CONCAT( '1', TRIM( Code ) )
  where LEN( TRIM( CODE ) ) < 6
+7
source

For SQL Server, and assuming the Code column is a character data type, you can do the following

UPDATE myTable
SET Code = '1' + Code
WHERE LEN(Code) < 6
+1
source

Source: https://habr.com/ru/post/1781083/


All Articles