Use CONCAT to adjust the amount of php mysql numbers

Asked

Viewed 423 times

5

I need to complete the amount of numbers to display on the screen, what would that be. a amount which would be an example: 20 need to fill 15 characters with 0,

Example: 000000000000020.

A friend of mine showed me in Excel a way that he uses the CONCAT and then takes only the last 15 numbers,with that the id can be 1,100,100 etc ... that it will always be 15 numbers that would be the zeroes plus the quantity.

I looked here on the site and also on the internet but I could not do what I need,.

This result I bring from Mysql, if it is easier in Mysql I can also use.

Here is an example of the query I tried to make:

SELECT 
 MAX(cup_id)             AS CUPOM,
 MAX(DATE_FORMAT(c.cup_data_hora, '%d/%m/%Y'))    AS DATA,
 MAX(c.cup_data_hora)    AS HORA,
 MAX(l.loja_num)         AS LOJA_NUM,
 MAX(l.loja_desc)        AS LOJA_NOME,
 MAX(l.loja_end)         AS END_LOJA,
 MAX(u.usu_cod)          AS USU_ID,
 MAX(u.usu_nome)         AS USU_NOME,
 MAX(v.vas_cod_emporium) AS COD_EMPORIUM,
 MAX( v.vas_desc)        AS VASILHAME,
 MAX(c.cup_quantidade)   AS QUANTIDADE,
 CONCAT(c.cup_quantidade,'000000000000') AS QUANTIDADE_15
FROM cupom  AS c
 LEFT JOIN loja      AS l ON c.cup_loja      = l.loja_num
 LEFT JOIN vasilhame AS v ON c.cup_vasilhame = v.vas_id
 LEFT JOIN usuarios  AS u ON c.cup_usu_id    = u.usu_cod
   WHERE cup_loja =4** 

  • vc want to set a number of with fixed digits, for example 5, if the number is 20 he put 3 zeros left?

  • 1

    Already tried to use the field in the table with ZEROFILL ?

  • @rray that’s right

  • @Zoom , I got with the help of our colleague rray ,more then I will see this part of you too,thanks for the return.

1 answer

9


Use the function lpad() from Mysql to add a 'prefix' to the left of the string, the first argument can be the field that will be modified, the second is the length(4 characters) and the last is the character that will be added.

SELECT lpad('1',4,0)
SELECT lpad('10',4,0)
SELECT lpad('100',4,0)
SELECT lpad('1000',4,0)

The exits are:

0001 
0010
0100
1000

Example - sqlfiddle

  • All right, thanks again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.