Pergunta

I am trying to sort a string column data as numeric. I am getting error like:

ERROR: invalid input syntax for integer: "HEI001"
SQL state: 22P02

I have tried with:

CAST({COL_NAME} AS integer ) ASC

NULLIF({COL_NAME}, '')::int 

But, it didn't work. Please help me. Thanks.

Foi útil?

Solução

You should probably first check whether the value is numeric (using regular expression) and then cast it since HEI001 can no way be casted to integer unless you are trying to get it's corresponding ASCII value (as @Clodoaldo have already commented)

CASE WHEN COL_NAME ~ '^[0-9]+$' THEN cast(COL_NAME as integer) END
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top