Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres (ERROR: column "date_deadline" is of type date but expression is of type text)

Tags:

postgresql

update res_user set date_of_birth= '1991-07-30' where name = 'USER1';

This work but can update just one row.

In my case I want to update multiple rows with different data in the same query so I wrote that, it doesn't work unfortunately.

Query:

update res_user as ru set
date_of_birth = n.date_of_birth
from (values
    ('1991-07-30','User1'),
    ('1980-06-30','User2'),
    ('1975-02-12','User3'),
) as n(date_of_birth, name)
where n.name = ru.name;

Result:

ERROR:  column "date_of_birth" is of type date but expression is of 
type text
LINE 2:     date_of_birth = n.date_of_birth
                        ^
HINT:  You will need to rewrite or cast the expression.
like image 233
Y.salama Avatar asked Mar 06 '23 05:03

Y.salama


1 Answers

You need to cast:

update res_user as ru set
date_of_birth = n.date_of_birth
from (values
    ('1991-07-30'::date,'User1'),
    ('1980-06-30'::date,'User2'),
    ('1975-02-12'::date,'User3'),
) as n(date_of_birth, name)
where n.name = ru.name;
like image 198
Lukasz Szozda Avatar answered May 11 '23 01:05

Lukasz Szozda