PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Altere a string vazia para NULL quando a coluna tiver a restrição DATE


Use NULLIF em sua instrução INSERT:
INSERT INTO your_table (cols..., some_date) VALUES (..., NULLIF(your_input_field, ''))

Se você deseja inserir NULL se o valor em questão for qualquer um de vários valores, pode ser mais fácil usar uma instrução CASE:
INSERT INTO your_table (cols..., some_date)
VALUES (..., CASE WHEN your_input_field IN ('', '#', '-', '--', '??') THEN NULL ELSE your_input_field END)

Poderia fazer o mesmo com um array também, se for mais fácil:
INSERT INTO your_table (cols..., some_date)
VALUES (..., CASE WHEN your_input_field = ANY('{"",#,-,--,??}'::TEXT[]) THEN NULL ELSE your_input_field END)