How to remove the last character from a field in Firebird SQL

Asked

Viewed 3,504 times

2

I have a field in a table that should only contain 4 characters, I realized that the same is with 5 characters and the last is the number zero.

Example:

Código Errado: 45380
Código Correto = 4538

Delete the last character of a field in Firebird SQL?

  • What have you tried? this field is int or string?

3 answers

1


You can use CHARACTER_LENGTH:

UPDATE minhaTabela
SET    minhaColuna = Substring(minhaColuna FROM 1 FOR 4)
WHERE  Character_length(minhaColuna) = 5  

This way only where there are 5 characters will be removed the last.

  • Gabriel Rodrigues tip worked. Thanks!

  • @alexrdantas that good that it went all right, use Firebird for a couple of years now!

0

Just make a UPDATE by dividing the value by 10.

update minhaTabela
set Campo = Campo/10
where [...]

0

Have you tried using the method strlen?

   UPDATE suatabel SET campo = SUBSTR(campo , 1, strlen(campo)-1)
    where strlen(campo) = 5;
  • What happens to values that have 6 characters?

  • he didn’t mention it in the question

  • I know, but I’m trying to make a question in order to improve your answer. Agree that it breaks your code?

  • Ready solved.

  • 1

    Felipe, don’t get me wrong, I’m just trying to help you get better. Do you agree that in this case it will also only work for those who own 5? What happens to the 6 or 7? I know he didn’t mention this, but what if they do exist? Wouldn’t it be better to continue with > 4 and in the SUBSTR put the fixed value to 4?

  • yes, just put it in a loop or case, but how do I know what it has in the string? it may have a number 3233000000000000000000,

  • Exactly, it can have this value, and to not need to make a loop or anything like that, just use SUBSTR(campo, 1, 4). Makes sense to you?

  • Depends, if he wants to remove anything from the end, yes, but what if it’s not a ZERO?

Show 3 more comments

Browser other questions tagged

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