Add a number in front of all records

3

A question arose when assembling an sql statement. I have several records in a table called db_contrato , I wanted to add the number 0 in front of all the records, for now I defined the contract column as varchar (50).

db_contract

id|contrato  
1 |3094  --> 03094   
2 |4058  --> 04058   
3 |2020  --> 02020  
4 |1620  --> 01620
    
asked by anonymous 16.01.2018 / 13:56

2 answers

4

Use the concat() function to add zero to left.

SELECT concat(0, contrato) FROM tabela

Example - sqlfiddle

Related:

Use CONCAT to adjust the number of php mysql numbers

    
16.01.2018 / 14:02
7

If you want a fixed number of houses:

SELECT LPAD( campo, 6, '0') AS comzeros;

If you want a zero only:

SELECT CONCAT( '0', campo) AS umzero;

If you have the space problem before, you can do a CAST:

SELECT LPAD( CAST(campo AS UNSIGNED) ....
    
16.01.2018 / 14:02