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.

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top