Insert X amount of characters in SQL Server field

Asked

Viewed 3,721 times

4

Hello, I have two banks BDMCOM1 and BDMCOM1_V3_ALEA, both of which have a table called Saldodeproducts and the field called Codigodoproduto, in the table of the bank BDMCOM1, Codigodoproduto has 5 digits, in the table of the other bank the codeDProduct has 8 digits...

I would like to select all table data SaldoDeProdutos, which has a column CodigoDoProduto whose field format is something like 00001, and insert into the other bank table BDMCOM1_V3_ALEA already including 3 zeroes left to complete the 8 digits.

1 answer

5


If you want to enter the zeros in the selection, you can do so:

SELECT A.*, REPLICATE('0', 8 - LEN(B.CODIGO)) + B.CODIGO
FROM TABELA_A A
INNER JOIN TABELA_B B ON (...)

The idea is that it doesn’t matter if there are 3 or less zeros missing. REPLICATE this way counts the missing zeroes and puts them to you.

See more about the function REPLICATE here.

The insertion would be something like this:

INSERT TABELA_B (/* Campos aqui */)
SELECT REPLICATE('0', 8 - LEN(A.CODIGO)) + A.CODIGO as CODIGO, -- Mais campos aqui
FROM TABELA_A
WHERE -- Alguma condição aqui.

I’m guessing the column guy CODIGO supports zeros on the left, such as varchar or nvarchar.


EDIT

Formatting the answer to the rest of the information looks like this:

INSERT BDMCOM1_V3_ALEA (CODIGODOPRODUTO, /* Demais campos aqui */)
SELECT REPLICATE('0', 8 - LEN(A.CODIGODOPRODUTO)) + A.CODIGODOPRODUTO as CODIGODOPRODUTO, -- Mais campos aqui
FROM SaldoDeProdutos
WHERE -- Alguma condição aqui, não é obrigatório este where.
  • Gypsy, it didn’t work out here, let me go into it... A bank eh BDMCOM1 and has the table Saldodeproducts and a field called Codigodoproduct with varchar 5, the other bank is the BDMCOM1_V3_ALEA and has the table Saldodeproduct and the field Codigodoproduct with varchar 8. I want to take for example the 00001 of BDMCOM1 and put in BDMCOM1_V3_ALEA so: 00000001. You can edit the answer in this pattern?

  • Can you please edit your question with these details?

  • Okay, already Edited out.

  • @Jadsonmedeiros See if it’s clearer now.

  • 1

    If I didn’t have the light on.

  • Incorrect syntax near the keyword 'FROM'.

  • Yes, but did you put the rest of the columns or did you just copy and paste what I put?

  • I pasted the code and replaced a few things, then the error was only in FROM, in case insert more columns is optional and no error would occur if you have only one, no?

  • 2

    The amount of columns in the INSERT must be equal to the number of columns of SELECT. There must be no comma between the last column and FROM.

  • Blz, but late I see it here, obg.

  • Gypsy, it worked, obg, it was just the same comma, but now I came across a field with quantity 1,00 and another field 1,000, in case I tried to adapt this excerpt: REPLICATE('0', 4 - LEN, and did not roll, in this case it is to take the 1,000 and send to the other bank as 1,00.

  • I think it’s worth asking another question, no?

Show 7 more comments

Browser other questions tagged

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