How to send values with Zeros to left SQL SERVER

Asked

Viewed 1,228 times

0

When trying to send values with zero on the left, when checking in the database I receive the values in zeros. Ex: 000431, appears in the database as 431. This value is being placed in the $prefix variable

Follow the code:

    <?php


$bomba1 = $_POST["bomba"]; 
$bomba = floatval($bomba1);

$prefixo1 = strval($_POST["prefixo"]);

$combustivel = $_POST["combustivel"];

$prefixo2 = str_pad($prefixo1, 6, '0', STR_PAD_LEFT);

$prefixo = substr($prefixo2, -5);     

echo $bomba ;
    echo $prefixo ;
    echo $combustivel ;

$serverName = "GALAXY\BDTL";
$connectionInfo = array( "Database"=>"Abastecimento", "UID"=>"", "PWD"=>"" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
     die( print_r( sqlsrv_errors(), true));
}

$sql = "INSERT INTO Abastecimento (prefixo, empresa) VALUES ($prefixo, 'TT' )";
//$params = array(1, "some data");

//$stmt = sqlsrv_query( $conn, $sql, $params);
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}else{
    print_r("Gravação concluída");
}
?>
  • 1

    If the data type is numerical then zeros on the left are not significant. If you just want to display with zeros on the left just format properly when displaying.

1 answer

0

can use the following function:

replicate(string expression, integer expression);

example:

select empid,          
       replicate('0', 6 - len(salary)) + cast (salary as varchar) as salary
from   emp;  

empid       salary
----------- -----------
1           000300
2           000030
3           500000
4           001000

(4 row(s) affected)

follows the source of these examples: https://www.tech-recipes.com/rx/30469/sql-server-how-to-left-pad-a-number-with-zeros/


For php use the function str_pad:

<?php
$input = "300";
print str_pad($input, 10);                      // produz "300     "
print str_pad($input, 10, "0", STR_PAD_LEFT);  // produz "0000000300"
print str_pad($input, 10, "0", STR_PAD_BOTH);   // produz "000300000"
print str_pad($input, 6 , "0");               // produz "300000"
?>
  • Opa, I found this type of help but I do not know if it applies to my problem, it seems that this example would be to change inside Sql, what I need is to send already formatted by PHP.

  • put in php the function

  • Yeah, that’s in the code I sent, but it didn’t work well.

  • guy does the following UNSIGNED poe on the database table and uses the anointing I passed to sql that solves. this is the right way to do it because besides understanding that it is an INT or FLOAT or DECIMAL ( whatever ) you can do operations directly

  • 1

    @Jasarorion seems to me a bad idea to change the table in the database to solve a code problem, besides that if the problem was only the zeroes on the left it means that you would want to store the value as if it were a text, then the most practical thing would be to convert the field to a varchar/char for example

  • problem q you want to solve part is bad modeling of the bank the fact that vc make it UNSIGNED already allows this change without you change the type of the field.

  • @Jasarorion, really, I can’t even change the database. I tried to convert but when I echo sql, I realized it was not converted to string. I used strval unsuccessfully :/

  • Someone with the solution ?

Show 3 more comments

Browser other questions tagged

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