0

I have a postgres table I loaded from a mongodb collection in postgres. Although the postgres column is of type 'bigint', there are rows that are larger than the max big int, so when I try to update another table from this table, it errors out. There are also bigint columns with illegal characters, such as "_2131441" which I cleared via

WHERE col_name !~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$';

How can I force cast an entire column to be valid according to it's type, and set it to null/none if otherwise?

1 Answer 1

2

Use a CASE expression:

CASE WHEN col_name !~ '^(\+|-)?[[:digit:]]+$'
     THEN NULL::bigint
     WHEN col_name::numeric NOT BETWEEN -9223372036854775808 AND 9223372036854775807
     THEN NULL::bigint
     ELSE col_name::bigint
END

Note that bigint is an integer and does not allow a decimal separator.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.