Postgres - SQL state: 22P02

0

In the following query:

SELECT t1.codigo
FROM tabla1 AS t1
INNER JOIN tabla2 AS t2 ON t1.codigo = t2.codigo
WHERE CASE WHEN POSITION( '-' IN t1.codigo ) = 0
          THEN CAST((t1.codigo) AS INTEGER)
          ELSE CAST(SUBSTRING( t1.codigo, 0, POSITION( '-' IN t1.codigo ) ) AS INTEGER)
      END
>
      CASE WHEN POSITION( '-' IN '1378' ) = 0 THEN
                CAST((SELECT codigo FROM tabla WHERE codigo = '1378') AS INTEGER)
           ELSE
                CAST(SUBSTRING( '1378', 0, POSITION( '-' IN '1378' ) ) AS INTEGER)
           END 
ORDER BY CASE WHEN POSITION( '-' IN t1.codigo ) = 0
          THEN CAST((t1.codigo) AS INTEGER)
          ELSE CAST(SUBSTRING( t1.codigo, 0, POSITION( '-' IN t1.codigo ) ) AS INTEGER)
      END
ASC LIMIT 1

It returns the following error:

  

ERROR: the input syntax is not valid for integer: «» SQL state: 22P02

What I want to achieve in this query is to show me the next number, if any, to the one that passed by variable (in this example 1378). In fact, what he does is convert a text field to an integer by removing the script if he has one.

Example number: 1183 or 110-1118

I use PostgreSQL as a database and the code fields are Character Varying of 200 characters.

The curious thing is that it happens to me in one of the schemes I have in the database because in another scheme, the original does not give me this error, they are cloned tables.

So I'm very confused with the subject.

I have tried without the where and it works, the problem is there but I can not find it.

Can you help me?

Thanks

    
asked by ruzD 19.04.2018 в 17:12
source

0 answers