Which SQL command to use to change a character of the vehicle plate - Mercosur Standard

Asked

Viewed 229 times

-1

I use a system that works with the Oracle 11g database and I have the table (Tab_plavei) with the string field (Plavei), format UU9999. I need to adjust this field to the new standard of Mercosur boards, format UU9U99.

Which command SQL should use to fix all the cards that have already been registered in the format UU9999 changing to the new format UU9U99, that is, change the 2nd numeric character or 5º of the string of the board, according to the standard table of Mercosul board: string 0 -> A; 1 -> B; 2 -> C; 3 -> D; 4 -> E; 5 -> F; 6 -> G; 7 -> H; 8 -> I and 9 -> J.

Thanks for the help!

  • 1

    would not be the update?

  • yes, but if possible the full code

  • ah want a complete code? sorry but I guess that’s not the purpose of the OS.. . here we help to solve a problem, not to solve completely... of a look at the command update, try how far you can solve, post your code here explaining what you did and what problem we can help you best

1 answer

0


Hello,

What you want to do is a little more complicated than it looks. Come on: to update the table, use the command UPDATE; to do what you want and modify a certain character in the string, the command STUFF() should work perfectly; already to replace the whole with the letter of the corresponding alphabet, try the command SUBSTRING().

Example of the STUFF function:

STUFF(ABC, @n, 1, 'X')

Take the value of the ABC field, start at the position @n, deletes 1 character and inserts character 'X' in position.

Example of the SUBSTRING function:

SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ', ABC, 1)

Replaces integer 1 by A, 2 by B, 3 by C and so on.

Final code:

UPDATE tableName
SET PlaVei = CONCAT( -- Concatena tudo para formar a nova placa
    SUBSTRING(PlaVei, 1, 4), -- Caracteres antes da letra alterada
    SUBSTRING("ABCDEFGHIJKLMNIOPQRSTUVWXYZ", SUBSTRING(PlaVei, 5, 1), 1), -- Altera o inteiro pela letra
    SUBSTRING(PlaVei, 6, 7) -- Caracteres depois da letra alterada
)

I hope I helped, hug!

  • Lucas, from what I understand the code is as needed, but when running on oracle I get the error ORA-00909, can help me?

  • I managed to change the mask of this UU9999 field to UU9U99

Browser other questions tagged

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