Skip to content
Advertisement

How to cast varchar on update?

When I update t1 then "VALUE" field should increment but this field is set to VARCHAR and I would cast it to INTEGER:

UPDATE t1 SET "VALUE"="VALUE"+1 
WHERE NOT EXISTS (
  SELECT "VALUE" FROM t1 WHERE CAST(t1."VALUE" as INTEGER) = t2."VALUE");

Advertisement

Answer

You will need to cast "VALUE" to an integer to be able to increment it, so:

UPDATE t1 SET "VALUE" = CAST("VALUE" as INTEGER) + 1 

Firebird will automatically convert the INTEGER back to a VARCHAR on assignment.

See this dbfiddle.

That said, the proper way to handle this is to change you schema and use an INTEGER type for this column.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement