How to concatenate a value to enter it in an update?

1

I have a query, I'm trying to concatenate this update

update tdato set valor= (select max(valor)+1 from tdato)

the value that returns me plus a text, try trying to contact it in the following way:

update tdato set valor = ('AG-')+convert(varchar,(select max(valor)+1 from tdato))

But it has not worked for me and in documentation I do not find anything useful.

Beforehand, thanking your contributions.

    
asked by Brian Velez 05.12.2018 в 20:11
source

1 answer

1

Try as follows:

update tdato set valor = (select 'AG-' + Convert(Varchar, max(SUBSTRING(valor, CHARINDEX('-', valor, 1) + 1, len(valor) - CHARINDEX('-', valor, 1))) + 1) from tdato)

First you get the value field of your table, the text that is after "AG-" which can be, 1, 2, 3, etc, (since you mention that it is stored as AG-1, AG-2, AG-3, AG-n), at that value you add 1 and then concatenate the text AG- again.

    
answered by 05.12.2018 / 22:18
source